如何通过RESTful Web服务正确生成JSON?

时间:2020-12-10 19:34:59

I am writing a web service the first time. I created a RESTful web service based on Jersey. And I want to produce JSON. What do I need to do to generate the correct JSON type of my web service?

我是第一次写网络服务。我创建了一个基于Jersey的RESTful Web服务。我想制作JSON。如何生成正确的JSON类型的Web服务需要做什么?

Here's one of my methods:

这是我的一个方法:

@GET
@Path("/friends")
@Produces("application/json")
public String getFriends() {
    return "{'friends': ['Michael', 'Tom', 'Daniel', 'John', 'Nick']}";
}

Is it sufficient that I simply point out annotation @Produces("application/json") for my method? Then this method may return any type of object? Or only String? Do I need additional processing or transformation of these objects?

我只是为我的方法指出@Produces(“application / json”)注释就足够了吗?那么这个方法可能会返回任何类型的对象?还是只有String?我是否需要对这些对象进行额外处理或转换?

Please help me as a beginner to deal with these issues. Thanks in advance!

请帮助我作为初学者来处理这些问题。提前致谢!

5 个解决方案

#1


29  

You can annotate your bean with jaxb annotations.

您可以使用jaxb注释来注释bean。

  @XmlRootElement
  public class MyJaxbBean {
    public String name;
    public int age;

    public MyJaxbBean() {} // JAXB needs this

    public MyJaxbBean(String name, int age) {
      this.name = name;
      this.age = age;
    }
  }

and then your method would look like this:

然后你的方法看起来像这样:

   @GET @Produces("application/json")
   public MyJaxbBean getMyBean() {
      return new MyJaxbBean("Agamemnon", 32);
   }

There is a chapter in the latest documentation that deals with this:

最新文档中有一章涉及此问题:

https://jersey.java.net/documentation/latest/user-guide.html#json

https://jersey.java.net/documentation/latest/user-guide.html#json

#2


5  

You could use a package like org.json http://www.json.org/java/

您可以使用像org.json这样的包http://www.json.org/java/

Because you will need to use JSONObjects more often.

因为您需要更频繁地使用JSONObjects。

There you can easily create JSONObjects and put some values in it:

在那里,您可以轻松创建JSONObjects并在其中放入一些值:

 JSONObject json = new JSONObject();
 JSONArray array=new JSONArray();
    array.put("1");
    array.put("2");
    json.put("friends", array);

    System.out.println(json.toString(2));


    {"friends": [
      "1",
      "2"
    ]}

edit This has the advantage that you can build your responses in different layers and return them as an object

编辑这样做的好处是,您可以在不同的层中构建响应并将它们作为对象返回

#3


3  

@GET
@Path("/friends")
@Produces(MediaType.APPLICATION_JSON)
public String getFriends() {

    // here you can return any bean also it will automatically convert into json 
    return "{'friends': ['Michael', 'Tom', 'Daniel', 'John', 'Nick']}";
}

#4


1  

@POST
@Path ("Employee")
@Consumes("application/json")
@Produces("application/json")
public JSONObject postEmployee(JSONObject jsonObject)throws Exception{
    return jsonObject;
}       

#5


0  

Use this annotation

使用此注释

@RequestMapping(value = "/url", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})

#1


29  

You can annotate your bean with jaxb annotations.

您可以使用jaxb注释来注释bean。

  @XmlRootElement
  public class MyJaxbBean {
    public String name;
    public int age;

    public MyJaxbBean() {} // JAXB needs this

    public MyJaxbBean(String name, int age) {
      this.name = name;
      this.age = age;
    }
  }

and then your method would look like this:

然后你的方法看起来像这样:

   @GET @Produces("application/json")
   public MyJaxbBean getMyBean() {
      return new MyJaxbBean("Agamemnon", 32);
   }

There is a chapter in the latest documentation that deals with this:

最新文档中有一章涉及此问题:

https://jersey.java.net/documentation/latest/user-guide.html#json

https://jersey.java.net/documentation/latest/user-guide.html#json

#2


5  

You could use a package like org.json http://www.json.org/java/

您可以使用像org.json这样的包http://www.json.org/java/

Because you will need to use JSONObjects more often.

因为您需要更频繁地使用JSONObjects。

There you can easily create JSONObjects and put some values in it:

在那里,您可以轻松创建JSONObjects并在其中放入一些值:

 JSONObject json = new JSONObject();
 JSONArray array=new JSONArray();
    array.put("1");
    array.put("2");
    json.put("friends", array);

    System.out.println(json.toString(2));


    {"friends": [
      "1",
      "2"
    ]}

edit This has the advantage that you can build your responses in different layers and return them as an object

编辑这样做的好处是,您可以在不同的层中构建响应并将它们作为对象返回

#3


3  

@GET
@Path("/friends")
@Produces(MediaType.APPLICATION_JSON)
public String getFriends() {

    // here you can return any bean also it will automatically convert into json 
    return "{'friends': ['Michael', 'Tom', 'Daniel', 'John', 'Nick']}";
}

#4


1  

@POST
@Path ("Employee")
@Consumes("application/json")
@Produces("application/json")
public JSONObject postEmployee(JSONObject jsonObject)throws Exception{
    return jsonObject;
}       

#5


0  

Use this annotation

使用此注释

@RequestMapping(value = "/url", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})