我试图生成一个excel文件,其中包含有关度量的数据。此文件将在Rest服务上返回。
@GET
@Path("{sensorId}/csv")
@Produces("text/csv")
public Response getVariablePointsInRangeAsCSV(@PathParam("sensorId") String sensorId,
@QueryParam("filter") FlowCause filter,
@QueryParam("from") String fromParam,
@QueryParam("until") String untilParam) throws Throwable {
List<Alivio> alivioList;
if (fromParam.isEmpty() || untilParam.isEmpty())
alivioList = aliviosService.getAliviosForSensor(sensorId, filter, null, null);
else
alivioList = aliviosService.getAliviosForSensor(sensorId, filter, PARAMS_DATE_FORMAT.parse(fromParam), PARAMS_DATE_FORMAT.parse(untilParam));
ByteArrayOutputStream baos = getExcelByteArrayOutputStreamForMeasures(sensorId,
CONSUMPTION_VARIABLE_NAME, alivioList);
return Response
.ok(baos.toByteArray())
.type("application/vnd.ms-excel")
.header("Content-disposition",
"attachment; filename=\"" + sensorId + "-" + CONSUMPTION_VARIABLE_NAME + ".xls\"")
.build();
}
private ByteArrayOutputStream getExcelByteArrayOutputStreamForMeasures(String sensorId,
String variableName, List<Alivio> listAlivios) throws IOException,
WriteException {
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("es", "ES"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(baos, wbSettings);
workbook.createSheet(sensorId + "-" + variableName, 0);
WritableSheet excelSheet = workbook.getSheet(0);
WritableCellFormat cellFormat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL,
10));
int index = 0;
for (Alivio alivio : listAlivios) {
excelSheet.addCell(new Label(0, index, EXCEL_TIMESTAMP_FORMAT.format(alivio.getStart())
, cellFormat));
excelSheet.addCell(new Label(1, index, EXCEL_TIMESTAMP_FORMAT.format(alivio.getEnd())
, cellFormat));
excelSheet.addCell(new Label(2, index, Double.toString(alivio.getVolume())
, cellFormat));
excelSheet.addCell(new Label(3, index, alivio.getCause().toString()
, cellFormat));
index++;
}
for (int x = 0; x < 4; x++) {
CellView columnView = excelSheet.getColumnView(x);
columnView.setAutosize(true);
excelSheet.setColumnView(x, columnView);
}
workbook.write();
workbook.close();
return baos;
}
当我调试我的项目时似乎生成文件很好,但是当Rest服务返回文件时会出现此错误。
2015-07-28 09:34:38,587 [1248010286@qtp-1681793106-10] ERROR c.s.j.s.container.ContainerResponse - A message body writer for Java class [B, and Java type class [B, and MIME media type application/vnd.ms-excel was not found
2015-07-28 09:34:38,588 [1248010286@qtp-1681793106-10] ERROR c.s.j.s.container.ContainerResponse - The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
2015-07-28 09:34:38,625 [1248010286@qtp-1681793106-10] ERROR c.g.s.c.r.j.CommonExceptionMapper - Exception in server
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class [B, and Java type class [B, and MIME media type application/vnd.ms-excel was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285) ~[iot-external-alivios-model-1.0.8.jar:na]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448) [iot-external-alivios-model-1.0.8.jar:na]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360) [iot-external-alivios-model-1.0.8.jar:na]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350) [iot-external-alivios-model-1.0.8.jar:na]
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) [iot-external-alivios-model-1.0.8.jar:na]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) [iot-external-alivios-model-1.0.8.jar:na]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) [iot-external-alivios-model-1.0.8.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) [servlet-api-2.5-20081211.jar:na]
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126) [jetty-6.1.22.jar:6.1.22]
at com.grupogimeno.iotsens.external.alivios.filters.StaticContentFilter.doFilter(StaticContentFilter.java:35) [classes/:na]
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) [jetty-6.1.22.jar:6.1.22]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [iot-external-alivios-model-1.0.8.jar:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [iot-external-alivios-model-1.0.8.jar:na]
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) [jetty-6.1.22.jar:6.1.22]
at com.grupogimeno.iotsens.auth.LoadAuthorizedUserFilter.doFilter(LoadAuthorizedUserFilter.java:49) [iot-external-alivios-model-1.0.8.jar:na]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [iot-external-alivios-model-1.0.8.jar:na]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [iot-external-alivios-model-1.0.8.jar:na]
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) [jetty-6.1.22.jar:6.1.22]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:65) [cas-client-core-3.1.12.jar:na]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [iot-external-alivios-model-1.0.8.jar:na]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [iot-external-alivios-model-1.0.8.jar:na]
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.Server.handle(Server.java:326) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) [jetty-6.1.22.jar:6.1.22]
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) [jetty-util-6.1.22.jar:6.1.22]
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class [B, and Java type class [B, and MIME media type application/vnd.ms-excel was not found
... 70 common frames omitted
谢谢!
答案 0 :(得分:1)
我解决了这个问题,将jersey-bundle
依赖项添加到POM。
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>