The JSON format is commonly used in RESTful web services to exchange messages. In this section, we will discuss how the Java API or JSON processing is used in a RESTful web service. First, add the RESTful web service dependency to pom.xml
:
<dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.0</version> </dependency>
Create a sample REST web service to test the JSON API. Create a resource class JsonResource
annotated with @PATH
to identify the URI path. Here's how we accomplish this:
package org.json; import java.io.StringReader; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.ws.rs.Path; import javax.json.*; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @Path("jaxrs") public class JsonResource { }
To package the resource class, create a class...