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!

Philiph Bruno
  • 413
  • 2
  • 4
  • 8

5 Answers5

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

plasma147
  • 2,191
  • 21
  • 35
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

TheBassMan
  • 115
  • 7
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']}";
}
bhanwar rathore
  • 328
  • 1
  • 3
  • 15
  • 3
    How does `@Produces("application/json")` differ from `@Produces(MediaType.APPLICATION_JSON)`? –  Jul 12 '16 at 06: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 '16 at 21:18
1
@POST
@Path ("Employee")
@Consumes("application/json")
@Produces("application/json")
public JSONObject postEmployee(JSONObject jsonObject)throws Exception{
    return jsonObject;
}       
ovejka
  • 999
  • 1
  • 16
  • 21
nasir ali
  • 11
  • 1
0

Use this annotation

@RequestMapping(value = "/url", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON})
Ajay Takur
  • 6,079
  • 5
  • 39
  • 55
  • Isn't this a Spring annotation? I don't think this is valid. – Entomo May 19 '16 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. – cassiomolin Feb 21 '18 at 11:58