0

I'm using Wildfly 10.0 and Apache POI to develop my application. I'd like to create and download an excel file but it does not seem to work.

This is my code:

package it.example.api;

import java.io.FileOutputStream;
import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import it.example.entity.Report;
import it.oss.base.media.MediaType;

@Path("/excel")
public class ExampleExcel {

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public void workbook() throws IOException {

     XSSFWorkbook workbook = new XSSFWorkbook();
     XSSFSheet sheet = workbook.createSheet("Java Books");
     Object[][] bookData = { { "Head First Java", "Kathy Serria", 79 }, {
     "Effective Java", "Joshua Bloch", 36 },
     { "Clean Code", "Robert martin", 42 }, { "Thinking in Java", "Bruce Eckel", 35 }, };

     int rowCount = 0;
     for (Object[] aBook : bookData) {
     Row row = sheet.createRow(++rowCount);
     int columnCount = 0;
     for (Object field : aBook) {
     Cell cell = row.createCell(++columnCount);
     if (field instanceof String) {
     cell.setCellValue((String) field);
     } else if (field instanceof Integer) {
     cell.setCellValue((Integer) field);
     }
     }
     }

     try (FileOutputStream outputStream = new
     FileOutputStream("Books.xlsx")) {
     workbook.write(outputStream);
     }
}

}

My server is started as you can see: Image1

I can't create and download the file when I go to http://localhost:8080/my-skeleton/api/excel/test, path is correct.

Can someone explain to me where I'm wrong?

Thanks in advance.

Edit The error is:

ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (default task-20) RESTEASY002010: Failed to execute: javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:8080/my-skeleton/api/excel/report
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:114)
at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:445)
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:257)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:194)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Jigen
  • 465
  • 1
  • 7
  • 20

1 Answers1

3

Try to change the content type that you want to download from json to the xls file

@GET
@Path("/get")
@Produces("application/vnd.ms-excel")

https://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/

Chit Khine
  • 830
  • 1
  • 13
  • 34
  • Where can I change this? – Jigen Sep 09 '16 at 10:33
  • i edited the answer, also for the exception, you should check this one http://stackoverflow.com/questions/27295720/javax-ws-rs-notfoundexception-could-not-find-resource-for-full-path-with-restea – Chit Khine Sep 09 '16 at 10:36
  • I tried and I got the same error: "Could not find resource for full path: http://localhost:8080/my-skeleton/api/excel/report" – Jigen Sep 09 '16 at 10:40
  • I think the problem is in not getting to your path. You should try and see the link http://stackoverflow.com/questions/27295720/javax-ws-rs-notfoundexception-could-not-find-resource-for-full-path-with-restea before trying to download the excel. Can you try printing out Logs to make sure you are in the api. – Chit Khine Sep 09 '16 at 10:44
  • I can correctly import class from library "org.apache.poi" so I really don't understand where is the problem.. – Jigen Sep 09 '16 at 10:49
  • Yes, I finally found that wildfly for some reason loads a different app with the same name, it seems work now – Jigen Sep 09 '16 at 12:16