@Consumes Annotation
The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client.
If @Consumes is applied at the class level, all the
response methods accept the specified MIME types by default. If applied at the
method level, @Consumes overrides any @Consumes annotations applied at the class level.
If a resource is
unable to consume the MIME type of a client request, the JAX-RS runtime sends back an HTTP 415
(“Unsupported Media Type”) error.
The value of @Consumes is an array of String of acceptable MIME types.
For example:
@Consumes({"text/plain,text/html"})
Steps:
1.
Add the Jackson dependency
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.
To make
Jersey support JSON mapping, declares “jersey-json.jar” in Maven pom.xml file.
|
pom.xml
|
<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>com.tushar.jersey.Project</groupId>
<artifactId>com.tushar.jersey.Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>JAX-RS</name>
<description>Rest
service </description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
</project>
|
2.
Integrate JSON with Jersey (in web.xml)
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>com.tushar.jersey.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>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.tushar.jersey.Project</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-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
|
3.
Create a POJO class
|
package
com.tushar.jersey.Project;
public
class Movie {
String
actor;
String
director;
public
String getActor() {
return
actor;
}
public
void setActor(String actor) {
this.actor
= actor;
}
public
String getDirector() {
return
director;
}
public
void setDirector(String director) {
this.director
= director;
}
@Override
public
String toString() {
return
"Movie [actor=" + actor + ",
director=" + director + "]";
}
}
|
4.
Create Service class
Annotate the method
with @Produces(MediaType.APPLICATION_JSON). Jersey will use
Jackson to handle the JSON conversion automatically.
|
package
com.tushar.jersey.Project;
import
javax.ws.rs.Consumes;
import
javax.ws.rs.GET;
import
javax.ws.rs.POST;
import
javax.ws.rs.Path;
import
javax.ws.rs.Produces;
import
javax.ws.rs.core.MediaType;
import
javax.ws.rs.core.Response;
import
com.tushar.jersey.Project.Movie;;
@Path("/json/MissionImpossible")
public
class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public
Movie getMovieJSON() {
Movie
movie = new Movie();
movie.setActor("Tom Cruise");
movie.setDirector("Steven Spielberg");
return
movie;
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public
Response createMoviekInJSON(Movie movie) {
String
result = "Movie saved : " + movie;
return
Response.status(201).entity(result).build();
}
}
|
5.
Execute the Code
6. To check the @Consume
annotation we need to create a Jersey Client class
|
package
com.tushar.jersey.Project;
import
com.sun.jersey.api.client.Client;
import
com.sun.jersey.api.client.ClientResponse;
import
com.sun.jersey.api.client.WebResource;
public class ConsumeClient {
public static void main(String[]
args) {
try {
Client client = Client.create();
WebResource webResource =
client
.resource("http://localhost:8080/com.tushar.jersey.Project/rest/json/MissionImpossible/get");
String input = "{\"actor\":\"Tom
Cruise\",\"director\":\"Spielberg\"}";
ClientResponse response =
webResource.type("application/json")
.post(ClientResponse.class, input);
if
(response.getStatus() != 201) {
throw new RuntimeException("Failed :
HTTP error code : "
+ response.getStatus());
}
System.out.println("Output from
Server .... \n");
String output =
response.getEntity(String.class);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
7. Run The Code:
Output



No comments:
Post a Comment