Sunday, May 24, 2015

@Produces Annotation

The information sent to a resource and then passed back to the client is specified as a MIME media type in the headers of an HTTP request or response. You can specify which MIME media types of representations a resource can respond to or produce by using the following annotations:

  •  javax.ws.rs.Consumes
  •   javax.ws.rs.Produces


By default, a resource class can respond to and produce all MIME media types of representations specified in the HTTP request and response headers.

@Produces Annotation
The 
@Produces annotation is used to specify the MIME media types or representations a resource can produce and send back to the client. If @Produces is applied at the class level, all the methods in a resource can produce the specified MIME types by default. If applied at the method level, the annotation overrides any @Produces annotations applied at the class level.
If no methods in a resource are able to produce the MIME type in a client request, the JAX-RS runtime sends back an HTTP “406 Not Acceptable” error.

Example: 
@Produces("image/png"): for downloading png image file.

Steps:

1.       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>
                                <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>


2. FileDownload.java


package com.tushar.jersey.Project;


import java.io.File; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/files")
public class FileDownload {

                      private static final String FILE_PATH_IMAGE = "d:\\image.jpg";
                      
                    @GET 
                    @Path("/image"
                    @Produces("image/png"
                    public Response getImageFile() { 
                        File file = new File(FILE_PATH_IMAGE); 
                        ResponseBuilder response = Response.ok((Object) file); 
                        response.header("Content-Disposition","attachment; filename=\"javatpoint_image.png\""); 
                        return response.build(); 
                  
                    } 
                                      
}


URL : http://localhost:8080/com.tushar.jersey.Project/rest/file/image
















Clicking Open will open the file and save will save the file in the defined destination.

When Clicked the file will show the image file:



















No comments:

Post a Comment