21

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?

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?

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

5 Answers 5

32

You can annotate your bean with jaxb annotations.

  @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

1
  • Thanks for pointing out the no-param constructor part. That was important !
    – simplysiby
    Mar 16, 2017 at 8:22
5

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

Because you will need to use JSONObjects more often.

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

 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

4
@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']}";
}
2
  • 3
    How does @Produces("application/json") differ from @Produces(MediaType.APPLICATION_JSON)?
    – user5547025
    Jul 12, 2016 at 6:40
  • 3
    It's exactly the same thing. MediaType.APPLICATION_JSON is a constant whose value is equal to application/json. Using a constant is less error-prone so a better choice
    – Adrien
    Oct 25, 2016 at 21:18
1
@POST
@Path ("Employee")
@Consumes("application/json")
@Produces("application/json")
public JSONObject postEmployee(JSONObject jsonObject)throws Exception{
    return jsonObject;
}       
0

Use this annotation

@RequestMapping(value = "/url", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON})
2
  • Isn't this a Spring annotation? I don't think this is valid.
    – Entomo
    May 19, 2016 at 15:03
  • The OP is pretty clear: "I created a RESTful web service based on Jersey." The @GetMapping annotation is not part of the Jersey API. It's a Spring MVC annotation. Feb 21, 2018 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.