In the previous section, we implemented the RESTful web service using JAXB. As mentioned earlier, JEE 8 has added a new specification for JSON binding, called JSON-B. In this section, we will learn how to modify our web service to use JSON-B.
There is really not much that we need to change in the code to switch from JAXB to JSON-B. We will need to use the @JsonbProperty annotation of JSON-B to specify field binding in the Course class, instead of the @XmlAttribute annotation of JAXB. Then, we will need to add Maven dependencies to include libraries that provide JSON-B APIs and its implementations. Replace the dependencies section in pom.xml with the following:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
Dependency on jersey-container-servlet has not changed. However, we have replaced dependency on jersey-media-json-jackson with jersey-media-json-binding and jersey-hk2. The Jersey framework automatically handles conversion of Java objects to JSON when the web service method is annotated with:
@Produces (MediaType.APPLICATION_JSON)
This is specified in the CourseService class.
A separate project for this section, named CourseManagementREST-JSONB, is made available in the accompanying source code for this chapter.