消费JSON post Jersey

时间:2015-06-12 19:57:44

标签: json rest post gwt jersey

我的应用程序有两个模块 - 前端和后端。 为了制作前端,我使用GWT框架。 在后端我用过泽西岛。 这两个模块应该通过REST在SOA中进行通信, 数据交换格式 - JSON。 我的问题是如何从GWT接收JSON。 我的代码看起来如下,但它不起作用,我不知道为什么。 所以我卡住了。我正在通过RequestBuilder在GWT中发送JSON:

private void sendServerProperties() {
    String path = FrontendModuleConfiguration.runningBackendServerAddress;
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
    try {
        builder.setHeader("Content-Type","application/x-www-form-urlencoded");

        String v1 = POP3ServerAdressField.getText();
        String v2 = addressField.getText();
        String v3 = passwordField.getText();

        final JSONObject serverPropertiesJSON = new JSONObject();
        serverPropertiesJSON.put("host", new JSONString(v1));
        serverPropertiesJSON.put("user", new JSONString(v2));
        serverPropertiesJSON.put("password", new JSONString(v3));

        // try {
        Request response = builder.sendRequest(serverPropertiesJSON.toString(), new RequestCallback() {

                    public void onError(Request request, Throwable exception) {
                        Window.alert("There was an error!" + exception.getMessage());
                    }

                    public void onResponseReceived(Request request, Response response) {
                        System.out.println("------ LOG ------");
                        System.out.println("Below JSON has been sent:");
                        System.out.println(serverPropertiesJSON.toString());
                    }});
    } catch (RequestException e) {
        Window.alert("Unable to build the request.");
    }
}

我尝试通过Jersey检索此JSON:

@Path("/emails")
public class EmailHandler {
	
	private final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    @POST
    @Path("/getemails")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response checkEmails(ServerProperties serverProperties){
    	String output = serverProperties.toString();
    	return Response.status(200).entity(output).build();
    }}

我的POJO看起来像这样:

public class ServerProperties {

	private String host;
	private String user;
	private String password;
	
	private String getHost() {
		return this.host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public String getUser() {
		return this.user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		return new StringBuffer(" Host : ").append(this.host)
		                .append(" User : ").append(this.user)
		                .append(" Password : ").append(this.password).toString();
	}
	
	public ServerProperties(){
		
	}
	
	public ServerProperties(String host, String user, String password){
		this.password=password;
		this.host=host;
		this.user=user;
	}

}

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>com.wp.projekt.tomasz.murglin.backend</display-name>
  <welcome-file-list>
    <welcome-file>readme.html</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.wp.projekt.tomasz.murglin.backend</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:1)

很明显,JSON不是form-urlencoded:

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
try {
    builder.setHeader("Content-Type","application/x-www-form-urlencoded");

...

@Consumes(MediaType.APPLICATION_JSON)