Download PDF file using JAX-RS RESTful Webservice


Warning: mysqli_query(): (HY000/1030): Got error 28 from storage engine in /home/ehowtono/public_html/wp-includes/wp-db.php on line 2030

In this tutorial we are going to see about how to download pf file using JAX-RS RESTful web service @Produces(“application/pdf”) annotation.

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey 2.x dependency in 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
        <jersey2.version>2.19</jersey2.version>
        <jaxrs.version>2.0.1</jaxrs.version>
    </properties>
  <dependencies>
      <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${jaxrs.version}</version>
        </dependency>
        <!-- Jersey 2.19 -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

DownloadPDFFile.java

package com.ehowtonow.webservice;

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://ehowtonow//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.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

Configure Jersey 2.x Servlet dispatcher

<!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>Restful Webservice Example</display-name>
	<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
			<param-value>com.ehowtonow.webservice</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>

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

http://localhost:8080/RestTutorials/rest/pdffiledownload/pdf-file


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Thirukural – Aalvinaiyutaimai – Kural 619
  2. Java – Executors Class
  3. TNPSC Librarian 2019 Library and Information Science Question with Answer PDF
  4. TNPSC Junior Inspector in Co-operative Department 2019 GENERAL Studies Question and Answer PDF
  5. Thirukural – Vinaiththitpam – Kural 664
  6. Thirukural – Kanvidhuppazhidhal – Kural 1177
  7. TNPSC Study Materials Geography Tamil 25
  8. Thirukural – Alararivuruththal – Kural 1146
  9. TNPSC Assistant Statistical Investigator 2015 Answer Key PDF
  10. TNPSC Assistant Director Of Industries And Commerce 2012 Commerce Question And Answer PDF
  11. Thirukural – Innaaseyyaamai – Kural 320
  12. Thirukural – Pasapparuparuvaral – Kural 1184
  13. விவசாயி கடைசி பக்க சேதியிலே – கவிதை
  14. Thirukural – Matiyinmai – Kural 601
  15. Thirukural – Sutrandhazhaal – Kural 530

Be the first to comment

Leave a Reply