Seems to work when sending/receiving Objects but not when sending strings.
Project uses gradle wrapper with gretty plugin for running the web-app under jetty.
./gradlew appRun
Then, just access in browser http://localhost:8080/jerseyGsonHandler/
index.html page is a simple page with an input where the user can put his name and then press one of two buttons:
- as string - it will submit the name as string and receive a hello message as string as well
public Response helloStr(String name) {
String msg = "Hello " + name + "!";
return Response.ok().entity(msg).build();
}
- as object - it will submit a json object with a name property and response will be a message object
public Response helloStr(User user) {
Message msg = new Message("Hello " + user.name() + "!");
return Response.ok().entity(msg).build();
}
System.out.println
was added to HelloResource
and GsonMessageBodyHandler
GSON is not being called for String...
For example, fill in Ricardo Almeida
in the input and click as string
.
In the network tab it can be seen that the request is "Ricardo Almeida"
and the response is Hello "Ricardo Almeida"!
instead of "Hello Ricardo Almeida!"
or, at most, 'Hello Ricardo Almeida!'
.
Dialog message shows error message Error parsing data
Shows the message from the resource but not from the GSON handler. The string includes double-quotes!!!
Got hello string request ["Ricardo Almeida"]
GSON handler is being called correctly
For example, fill in Ricardo Almeida
in the input and click as object
.
In the network tab it can be seen that the request is {"name":"Ricardo Almeida"}
and the response is { "message": "Hello Ricardo Almeida!" }
.
Dialog message correctly shows Got: Hello Ricardo Almeida!
Shows the 3 messages as expected (read from GSON handler, resource and write from GSON handler).
GsonMessageBodyHandler.readFrom() -= User[name=Ricardo Almeida] =-
Got hello object request [User[name=Ricardo Almeida]]
GsonMessageBodyHandler.writeTo() -= Message[message=Hello Ricardo Almeida!] =- {"message":"Hello Ricardo Almeida!"}