Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 November 2013

Download PDF file using JAX-RS RESTful Webservice


In this tutorial we are going to see how to download pf file using RESTful web service
@Produces("application/pdf") annotation
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder add jar
4. Create package called com.javatutorialscorner.jaxrs.downloadfile under RESTful-WebService
5. Create Java class DownloadPDFFile under com.javatutorialscorner.jaxrs.downloadfile package
DownloadPDFFile.java
package com.javatutorialscorner.jaxrs.downloadfile;

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("/pdffiledownload")
public class DownloadPDFFile {

private static final String FILE_PATH = "C://jtc//java-tutorials.pdf";
@GET
@Path("/pdf-file")
@Produces("application/pdf")
public Response downloadFile() {
File file = new File(FILE_PATH);
ResponseBuilder responseBuilder = Response.ok((Object)file);
responseBuilder.header("Content-Disposition",
"attachment; filename=\"java-tutorials-corner.pdf\"");
return responseBuilder.build();
}
}

@Produces("application/pdf") used to set output response is pdf file.
Content-Disposition in response header to tell user agent to pop up download box to download file
6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
web.xml

01.<?xml version="1.0" encoding="UTF-8"?>
03.  <display-name>JAX-RS-Path</display-name>
04.<servlet>
05.  <servlet-name>jersey-serlvet</servlet-name>
06.  <servlet-class>
07.                     com.sun.jersey.spi.container.servlet.ServletContainer
08.                </servlet-class>
09.  <init-param>
10.       <param-name>com.sun.jersey.config.property.packages</param-name>
11.       <param-value>com.javatutorialscorner.jaxrs.downloadfile</param-value>
12.  </init-param>
13.  <load-on-startup>1</load-on-startup>
14. </servlet>
15.  
16. <servlet-mapping>
17.  <servlet-name>jersey-serlvet</servlet-name>
18.  <url-pattern>/rest/*</url-pattern>
19. </servlet-mapping>
20.</web-app>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RESTful-WebService/rest/pdffiledownload/pdf-file

Output
pdf

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • RESTful Web service with Apache HttpClient In this tutorials we are going to see the how to access the RESTful web service using Apache HttpClient. Here i am going to create two clients to access GET and POST request Add httpcore-4.1.jar, httpclient-4.1.1.jar, comm… Read More
  • File Upload using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to upload file using Jersey RESTful web service 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-WebServic… Read More
  • Download Excel file using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to download text file using RESTful web service @Produces("application/vnd.ms-excel") annotation 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Proje… Read More
  • RESTful Web service with RESTEasy Client In this tutorials we are going to see the how to access the RESTful web service using  RESTEasy client. Here i am going to create two clients to access GET and POST request GET Request Refer this example for creatin… Read More
  • JAX-RS RESTful Web service to consume JSON and Produce XMLIn this tutorial we are going to see how to consume JSON and Produce XML using RESTful web service1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-W… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

1 comments:

Item Reviewed: Download PDF file using JAX-RS RESTful Webservice Rating: 5 Reviewed By: eHowToNow