0

我有 restyGWT+GXT 项目,它向服务器项目(Spring Boot)发送请求,所以,我的restyGWT+GXT 部分

buid.gradle:

...
compile 'org.fusesource.restygwt:restygwt:2.0.3'
compile 'javax.ws.rs:jsr311-api:1.1.1'

我在 restyGWT+GXT 部分的休息服务:

import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

public interface LoadHelloService extends RestService {

    @GET
    @Path("/rest/loadHelloService")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public void loadHelloService(MethodCallback<List<Hello>> callback);
}

豆你好.java:

public class Hello {

    private final String id;
    private final String name;

    @JsonCreator
    public Hello(@JsonProperty("id") String id, @JsonProperty("name") String name) {
        this.id = id;
        this.name = name;

    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

在 MainMenuPage ( implements IsWidget) 中:单击 menuButton1 向服务器项目发送请求(Spring Boot)

    @UiHandler("menuButton1")
    void selectOnMenu1(SelectEvent event) {
        ...
        restServerLoader.loadHelloListFromServer();        
    }

所以,RestServerLoader有方法的类loadHelloListFromServer

import com.google.gwt.core.client.GWT;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import org.fusesource.restygwt.client.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RestServerLoader {

   public void loadHelloListFromServer() {
        String pageBaseUrl = "http://127.0.0.1:8080/";
        Defaults.setServiceRoot(pageBaseUrl);

        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Control-Allow-Methods", "GET");

        Resource resource = new Resource(Defaults.getServiceRoot(), headers);

        LoadHelloService service = GWT.create(LoadHelloService.class);
        ((RestServiceProxy)service).setResource(resource);

        service.loadHelloService(new MethodCallback<List<Hello>>() {

            public void onSuccess(Method method, List<Hello> response) {
                MessageBox messageBox = new MessageBox("response (list) = " + response.toString());
                messageBox.show();
                //code your stuff here
            }

            public void onFailure(Method method, Throwable exception) {
                MessageBox messageBox = new MessageBox("exception = " + exception);
                messageBox.show();
                //code your stuff here
            }


        });
   }
}

所以,当我发送请求时,loadHelloService我有:org.fusesource.restygwt.client.FailedStatusCodeException: status code 0. :(((((((

我的服务器部分(Spring Boot)休息:

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {

    @Context
    private HttpServletResponse response;


    @OPTIONS
    @Path("loadHelloService") //The response for the preflight request made implicitly by the bowser
    public Response loadHelloPreflight() {
        Response response = Response.ok()
                .header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods", "POST, GET, UPDATE, OPTIONS")
                .header("Access-Control-Allow-Headers", "*")
                .header("Access-Control-Max-Age", "18000").build();

        return response;
    }

    @GET
    @Path("loadHelloService")
    public List<Hello> loadHelloList() {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "POST, GET, UPDATE, OPTIONS");
        response.addHeader("Access-Control-Allow-Headers", "*");

        List<Hello> list = new ArrayList<>();
        list.add(new Hello("1", "ronan"));
        list.add(new Hello("2", "john"));
        return list;
    }

}

所以,当我发送请求时,我输入了方法loadHelloPreflight,但是当我发送请求到 loadHelloList 我有:org.fusesource.restygwt.client.FailedStatusCodeException: status code 0.,为什么???:((,当我从浏览器 Postman Client 向服务器部分(Spring Boot)发送请求时- 一切都好!,我得到了 hellos 列表,但我想从restyGWT+GXT 部分做到这一点:(((请帮帮我。

4

1 回答 1

0

您是否忘记了下面的一些 cors 标题?

.header("Access-Control-Allow-Headers", "x-http-method-override");
于 2016-07-04T14:44:49.093 回答