简体   繁体   中英

Returning custom JSON with RESTful Jersey

It's my first try with Maven and Jersey. I have read quite a lot articles about JSON. I want to know if my problem is solvable without JSON simple and GSON.

When I visit localhost:8080/helloWorld/example1/example2/example3 , I get something like this

{"first": example1, "second": example2, "third":example3}

Which is nice for a start but I want to get a response like this:

{
  "firstMap": {"first": example1, "second": example2},
  "secondMap":{"third": example3}
}

I tried to make responseWrapper class, but it returns

{
  "firstMap": {"first": example1, "second": example2, "third": null},
  "secondMap":{"first": null, "second": null, "third": example3}
}.

I don't want these nulls to be displayed. How can I do it?

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("sample")
public class HelloWorldService {

    @Path("/helloWorld/{first}/{second}/{third}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public HelloWorld helloWorld(@PathParam("first") String first, @PathParam("second") String  second, @PathParam("third") String third) {
        return new HelloWorld(first, second, third);
    }
}

And also:

public class HelloWorld {

    private String first;
    private String second;
    private String third;

public HelloWorld(String first, String second) {
    this.first = first;
    this.second = second;

}

public HelloWorld(String third, String third) {
    this.third = third;

}

public HelloWorld(){
}

    public HelloWorld(String first) {
        this.first= first;
    }

    public String getFirst() {
        return first;
    }

    public String getSecond(){
        return second;
    }

    public String getThird(){
        return third;

If you need it just for this usage, you can try to mimic the class itself so that when it is serialized it will produced with the format you want to. Gson will allow you to write custom serialization and deserialization for the class, but then you lose the automation. This could be the code for the HelloWorld class:

public class HelloWorld {

    public class Data1
    {
        private String first;
        private String second;
            // getter and setters...
    }

    private Data1 firstMap;

    public class Data2
    {
        private String third;
            // getter and setters...
    }

    private Data2 secondMap;
    // ...
}

If you want a json of that form

{
  "firstMap": {"first": example1, "second": example2},
  "secondMap":{"third": example3}
}

The object that you return from your service should have this structure.

public class Root {
  public First firstMap;
  public Third secondMap;
}

public class First {
  public String first;
  public String second;
}

public class Third {
  public String third;
}

Then you can use a library like Genson , that plays nicely with Jersey. You just need Genson in your classpath and then it will get automatically enabled and handle json ser/de.

Gson has a GsonBuilder class to set formatting flags when constructing Gson main object. Look for setPettyPrinting() to format it in human readable and serializeNulls() for toggling printing null on. Printing nulls is off by default. When using it from web service the Jackson library gives you more control with the annotations and has better integration with spring. If you are using Spring which I highly recommend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM