Skip to content

i23098/jerseyGsonHandler

Repository files navigation

jerseyGsonHandler

Attempt to use jersey + GSON

Seems to work when sending/receiving Objects but not when sending strings.

Build / Run

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();
        }

testing

System.out.println was added to HelloResource and GsonMessageBodyHandler

as string

GSON is not being called for String...

Browser

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

Server

Shows the message from the resource but not from the GSON handler. The string includes double-quotes!!!

Got hello string request ["Ricardo Almeida"]

as object

GSON handler is being called correctly

Browser

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!

Server

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!"}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published