简体   繁体   English

使用RESTful Jersey返回自定义JSON

[英]Returning custom JSON with RESTful Jersey

It's my first try with Maven and Jersey. 这是我第一次尝试使用Maven和Jersey。 I have read quite a lot articles about JSON. 我已经阅读了很多关于JSON的文章。 I want to know if my problem is solvable without JSON simple and GSON. 我想知道如果没有简单的JSON和GSON,我的问题是否可以解决。

When I visit localhost:8080/helloWorld/example1/example2/example3 , I get something like this 当我访问localhost:8080/helloWorld/example1/example2/example3 ,我得到这样的东西

{"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 我试图创建responseWrapper类,但它返回

{
  "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. Gson将允许您为类编写自定义序列化和反序列化,但随后您将失去自动化。 This could be the code for the HelloWorld class: 这可能是HelloWorld类的代码:

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 如果你想要一个这种形式的json

{
  "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. 然后你可以使用像Genson这样的图书馆,与泽西岛很好地搭配。 You just need Genson in your classpath and then it will get automatically enabled and handle json ser/de. 您只需在类路径中使用Genson,然后它将自动启用并处理json ser / de。

Gson has a GsonBuilder class to set formatting flags when constructing Gson main object. Gson有一个GsonBuilder类来在构造Gson主对象时设置格式化标志。 Look for setPettyPrinting() to format it in human readable and serializeNulls() for toggling printing null on. 查找setPettyPrinting()以在人类可读和serializeNulls()格式化它以切换打印null。 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. 从Web服务中使用它时,Jackson库可以为您提供更多的注释控制,并且可以更好地与spring集成。 If you are using Spring which I highly recommend. 如果您使用Spring,我强烈推荐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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