There are many JSON libraries that make JSON works possible in a Java application. When responding HTTP response that should be in JSON format we should use the annotation:
@Produces(MediaType.APPLICATION_JSON)
With the Http Methods Get, Post, Delete, Put and Patch, we should consider the respective annotation, i.e. @GET, @POST, @DELETE, @PUT and @PATCH.
Since it is desirable that RESTful Java apps reply through JSON objects, we do responses in JSON format.
One way would be to write a String variable but use the curly brackets and backslashes as in
String var1 = "{\"Hello\":\"world\"}"; return Response.ok(var1).build();
Another way would be to use a library package like Gson. More reading can be found at: https://github.com/google/gson
JSONObject obj = new JSONObject(); obj.put("name", "foo"); String json = new Gson().toJson(obj); return Response.ok(json).build(); /* Returns: { "map": { "name": "foo" } } */
Its Maven dependency would be:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
Another technique is using the org.json’s JSONObject and to use its .toString at return time:
JSONObject obj = new JSONObject(); obj.put("name", "foo"); return Response.ok(obj.toString()).build();
Its dependency is as follows and more reading can be found at ttps://www.tutorialspoint.com/json/json_java_example.htm
<!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20190722</version> </dependency>
Java applications using the Jersey implementation for RESTful Java apps can use the Jersey media dependency. More reading on this can be found at: https://code.i-harness.com/en/q/120cba9 where we see that the right Maven dependency can help in returning JSON objects from Java variables:
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>{latest version}</version> </dependency>
For example to return a List variable in JSON format using the above dependency, we can write code where we use the @POST annotation, prepare a list of persons and return via Response.ok(persons).build.
@POST @Path("/all") @Consumes(MediaType.APPLICATION_JSON) @Produces({MediaType.APPLICATION_JSON}) public Response all() { Person p1 = new Person (1, "Bob", "bob@test.com", "0123456789", "123 Main St."); Person p2 = new Person (2, "Bob2", "bob@example.com", "0987654321", "456 Main St."); Person p3 = new Person (3, "Bob3", "bob@company.com", "0700123456", "789 Main Street"); List<Person> persons = new ArrayList<Person>(); persons.add(p1); persons.add(p2); persons.add(p3); //good resources: // https://stackoverflow.com/questions/41761082/convert-list-to-jsonobject-in-java-using-json-simple //https://code.i-harness.com/en/q/120cba9 //java - webservices - jersey return arraylist json System.out.println("List: " + persons); return Response.ok(persons).build(); //this works //return Response.ok().entity(persons).build(); //this works also } }
Other resources:
- Handling the error:
Exception in thread “main” javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
http://www.topjavatutorial.com/java/java-programs/java-api-json-processing/