Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How correctly produce JSON by RESTful web service?

Tags:

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!

like image 804
Philiph Bruno Avatar asked Nov 27 '12 23:11

Philiph Bruno


People also ask

How do you get a JSON object from a RESTful web service?

First, a String URL to call the RESTful Web Service, and second, the name of the class should return with the response. So, in just one line of code, it calls the RESTful web service, parses the JSON response, and returns the Java object to you.

How does REST API send JSON data?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

What is JSON RESTful web service?

RESTful web services REpresentational State Transfer, or REST, is a design pattern for interacting with resources stored in a server. Each resource has an identity, a data type, and supports a set of actions. The RESTful design pattern is normally used in combination with HTTP, the language of the internet.

What is JSON and how is it used in RESTful APIs?

JSON is based on a subset of the JavaScript Programming Language. Representative State Transfer (REST) is a client-server architectural style that uses the HTTP protocol in a simple and effective way. Systems that adhere to REST practices are often referred to as RESTful interfaces.

Which of the following code creates a RESTful web service to return JSON response?

Java RESTful Web Services With JSON Example Source CodeAPPLICATION_XML, MediaType. APPLICATION_JSON }) and we have already added the dependent JAR files via Maven to generate JSON response. Now if this web service receives a request with request header Accept: application/json , it will send out a JSON response.

Does REST API uses JSON?

The REST architecture allows API providers to deliver data in multiple formats such as plain text, HTML, XML, YAML, and JSON, which is one of its most loved features.


2 Answers

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

like image 192
plasma147 Avatar answered Oct 07 '22 19:10

plasma147


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

like image 32
TheBassMan Avatar answered Oct 07 '22 18:10

TheBassMan