如何将RestyGWT部分的请求发送到另一个服务器项目?

时间:2015-10-07 14:59:48

标签: java rest request cors resty-gwt

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

buiid.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);
}

bean Hello.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客户端发送请求到服务器部分(Spring Boot)时 - 一切都很好!)我得到了hellos的列表,但我想从 restyGWT + GXT部分做到:(((请帮帮我。

1 个答案:

答案 0 :(得分:0)

你有没有忘记像下面的一些角色?

.header("Access-Control-Allow-Headers", "x-http-method-override");