Overview
This article describes how to get a JSON response from the RESTful web services using jersey implementation.Jersey will use Jackson to convert Java objects to/form JSON.Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON.Jersey will use this API to the marshaling [converting the objects] process.
Add ‘jersey-json.jar‘ to your Maven pom.xml which includes all Jackson and other JSON supporting libraries.
pom.xml
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>RESTExceptionHandlingExample</groupId> <artifactId>RESTExceptionHandlingExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>WebServicesJAXRS</name> <description>Restfull Webservices</description> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency> </dependencies> <build> <sourceDirectory>RESTExceptionHandlingExample</sourceDirectory> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> |
In web.xml add “com.sun.jersey.api.json.POJOMappingFeature” as “init-param”
web.xml
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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"> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.codenuclear</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> |
Customer.java
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.codenuclear; public class Customer { private int custNo; private String custName; private String custCountry; public int getCustNo() { return custNo; } public void setCustNo(int custNo) { this.custNo = custNo; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustCountry() { return custCountry; } public void setCustCountry(String custCountry) { this.custCountry = custCountry; } } |
In the web service class, we need to annotate the method with @Produces(MediaType.APPLICATION_JSON). By doing so we are instructiong the service method that we are expecting the JSON output, thats it jersey will take care rest of the things.
JsonFromRestful.java
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.codenuclear; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/users") public class JsonFromRestful { @GET @Path("/{cusNo}") @Produces(MediaType.APPLICATION_JSON) public Customer produceCustomerDetailsinJSON(@PathParam("cusNo") int no) { /* * I AM PASSING CUST.NO AS AN INPUT, SO WRITE SOME BACKEND RELATED STUFF AND * FIND THE CUSTOMER DETAILS WITH THAT ID. AND FINALLY SET THOSE RETRIEVED VALUES TO * THE CUSTOMER OBJECT AND RETURN IT, HOWEVER IT WILL RETURN IN JSON FORMAT :-) * */ Customer cust = new Customer(); cust .setCustNo(no); cust .setCustName("codeNuclear"); cust .setCustCountry("India"); return cust; } } |
The application will start by hitting the following URL
http://localhost:8080/WebServices_JAX_RS/rest/users/200
That’s all for JSON response from the RESTful web services using jersey implementation, Hope you liked it. Keep Learning and Sharing 🙂 !!
I pay a quick visit each day a few websites and sites to read articles, except this blog offers quality based writing.|