Reputation:
My application has two modules - frontend and backend. In order to make frontend I use GWT framework. On the backend I've used Jersey. These two modules should communicate in SOA trough REST, data exchange format - JSON. My question is how can I receive JSON from GWT. My code looks like below but it is not working and I have no idea why. So I stuck. I'm sending JSON in GWT by RequestBuilder:
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.");
}
}
I try to retrieve this JSON by Jersey:
@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();
}}
My POJO looks like this:
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;
}
}
My 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>
Upvotes: 0
Views: 183
Reputation: 64561
Quite obviously JSON is not form-urlencoded:
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
try {
builder.setHeader("Content-Type","application/x-www-form-urlencoded");
…
@Consumes(MediaType.APPLICATION_JSON)
Upvotes: 1