In this article we will learn how to use JACKSON api in a jersey based rest web services project in java. Jersey uses Jackson to convert object to / form JSON. In this tutorial, we show you how to convert a “Track” object into JSON format, and return it back to user.
Tools used
Maven
JDK 1.8 (even 1.6 would work fine)
Eclipse/IntelliJ(you can use whichever you are comfortable with)
Tomcat Server 8
These steps are followed in this tutorial,
it’s Damn Easy !
Step 1: create a java web project in eclipse and convert it to maven project.
Step 2: add jersey jackson dependency in pom.xml and update the project.
Step 3: create an API class.
Step 4: in the web.xml add the servlet and servlet mapping.
Step 5: Just run the project, Hurray !
POM Dependencies
<properties>
<jersey2.version>2.28</jersey2.version>
<jaxrs.version>2.0.1</jaxrs.version>
</properties>
<dependencies>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<!-- Jersey 2.28 -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/
jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.28</version>
</dependency>
</dependencies>
Note: What happens when you do not add jersey-media-json-jackson jar in pom.xml ? The API when hit returns a SEVERE exception:
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.programmertoday.restjersey.model.Department, genericType=class com.programmertoday.restjersey.model.Department.
Because the resource is trying to produce an object of type json, but it does not find any convertor which converts Object into a json, hence adding the above jackson jar resolves this issue.
1. API code
package com.programmertoday.restjersey.api;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/api")
public class JerseyResource {
@GET
@Path("/department")
@Produces(MediaType.APPLICATION_JSON)
public Department getDepartment()
{
return new Department("IT","Information Technology");
}
}
2. Model Class
package com.programmertoday.restjersey.model;
public class Department {
private String depName;
private String depCode;
public Department(String a, String b) {
this.depCode = a;
this.depName = b;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
public String getDepCode() {
return depCode;
}
public void setDepCode(String depCode) {
this.depCode = depCode;
}
}
3. Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>RESTful_Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.programmertoday.restjersey.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
TEST the code
Add a tomcat server and run the project !!
Using any REST client OR any web browser like google chrome open the below url to hit the api.
http://localhost:8080/RESTful_Project/rest/api/department
{"depName":"Information Technology","depCode":"IT"}
Summary
In this article we learnt how to create a simple Jersey based REST API with JACKSON, which leverages us to convert the model class to JSON on the fly. There is no need to write a JSON Array explicitly for this. It’s Super simple and easy !.
Hope you liked the article !