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!
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.
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.
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With