JAX RS download excel file example

Click here to download eclipse supported ZIP file



This is index.html JSP file and it is used display the output for the application.




<html>
<body>
<h1>JAX-RS @Download excel Testing</h1>
<a href="./excel/get">click here</a><br>
</body>
</html>


This is ExcelService.java file having the service/business logic to call the DAO layer and get the information from database.



 

    
package com.cv.webservices.rest;

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;
/**
 @author Chandra Vardhan
 *
 */
@Path("/excel")
public class ExcelService {

  private static final String FILE_NAME = "testData.xlsx";

  @GET
  @Path("/get")
  @Produces("application/vnd.ms-excel")
  public Response getFile() {

    String fileName = getFile(FILE_NAME);
    File file = new File(fileName);
    ResponseBuilder response = Response.ok((Objectfile);
    response.header("Content-Disposition",
        "attachment; filename=testData.xlsx");
    return response.build();

  }

  private String getFile(String fileName) {
    // Get file from resources folder
    return getClass().getClassLoader().getResource(fileName).getPath();
  }
}


This is pom.xml file having the entries of dependency jars and information to build the application .



	
<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">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.cv.webservices.common</groupId> <artifactId>JAXRSDownloadExcelFileExample</artifactId> <packaging>war</packaging> <version>1.0</version> <name>JAXRSDownloadExcelFileExample Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>JBoss repository</id> <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> </dependency> </dependencies> <build> <finalName>JAXRSDownloadExcelFileExample</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>


This is web.xml deployment descriptor file and it describes how the web application should be deployed.



	

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Restful Web Application</display-name>

<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>

<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/excel/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


This is log4j.properties file having the entries for logging the information into the console/file.




#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

No comments:

Post a Comment