This tutorial explains how to use JSONP JSON provider with Jersey 2.x. JSONP is also auto-discoverable just like what we discussed in Jersey MOXy example.
Table of Contents JSONP maven dependencies/changes REST API code Model bean changes Manually adding JsonProcessingFeature
JSONP maven dependencies/changes
JSONP media module is one of the modules in Jersey 2.x where you don’t need to explicitly register it’s features e.g. JsonProcessingFeature. Once Jersey detects added it’s presence in class path, it automatically registers it. So just add JSONP dependency in pom.xml
does half work.
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-processing</artifactId> <version>2.19</version> </dependency>
REST API code
At Service side, where your APIs are written, you need to enable JSON media types using @Produces(MediaType.APPLICATION_JSON)
annotation.
JerseyService.java
@Path("/employees") public class JerseyService { @GET @Produces(MediaType.APPLICATION_JSON) public Employees getAllEmployees() { Employees list = new Employees(); list.setEmployeeList(new ArrayList<Employee>()); list.getEmployeeList().add(new Employee(1, "Lokesh Gupta")); list.getEmployeeList().add(new Employee(2, "Alex Kolenchiskey")); list.getEmployeeList().add(new Employee(3, "David Kameron")); return list; } }
Model bean changes
At model beans side, you don’t need to put any annotation or any configuration. It will work by default. You even don’t need to put any root annotation as well.
Employees.java
public class Employees { private List<Employee> employeeList; public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } }
Employee.java
public class Employee { private Integer id; private String name; public Employee() { } public Employee(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + "]"; } }
Manually adding JsonProcessingFeature
Though org.glassfish.jersey.jsonp.JsonProcessingFeature
is registered automatically, if you wish to register it manually, you can add it in configuration as below.
public class CustomApplication extends ResourceConfig { public CustomApplication() { register(JsonProcessingFeature.class); packages("com.howtodoinjava.jersey"); packages("org.glassfish.jersey.examples.jsonp"); register(LoggingFilter.class); property(JsonGenerator.PRETTY_PRINTING, true); } }
And add this Application class in web.xml
file.
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.howtodoinjava.jersey.CustomApplication</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>
Drop me your question and comments below.
Happy Learning !!
Reference : https://jersey.java.net/documentation/latest/media.html#json.json-p
Leave a Reply