【问题标题】:Returning an excel document via a Java REST service with Apache Wink使用 Apache Wink 通过 Java REST 服务返回 excel 文档
【发布时间】:2016-08-23 01:48:35
【问题描述】:

我需要从 Java REST 服务返回一个 Microsoft Excel 文件。我正在使用 WebSphere 8.5,它固有地使用 Apache Wink 作为它的 JAX-RS 实现;这是我无法改变的要求。我也在使用 Java 7 JDK。这是我收到的错误:

org.apache.wink.server.internal.handlers.FlushResultHandler handleResponse 系统找不到 javax.ws.rs.ext.MessageBodyWriter 或 DataSourceProvider 类 com.somewhere.else.message.core.BaseResponseMessage 类型和 应用程序/vnd.ms-excel 媒体类型。确保一个 javax.ws.rs.ext.MessageBodyWriter 存在于 JAX-RS 应用程序中 指定的类型和媒体类型。

这是我的 Java Resource 类方法:

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

    int fileSize = 0;

    byte[] reportByteArray = null;

    ResponseBuilder responseBuilder = null;
    InputStream report = null;

    BaseResponseMessage<InputStream> baseResponseMessage = new  
    BaseResponseMessage<InputStream>();

    Path reportPath = null;

    String localPath = "C:/Users/me/Report.xls";

    responseBuilder = Response.ok(baseResponseMessage); 

    responseBuilder.header("Content-Description", "File Transfer");
    responseBuilder.header("Content-Disposition", "attachment; 
         filename=Report.xls");
    responseBuilder.header("Content-Transfer-Encoding", "binary");
    responseBuilder.header("Connection", "Keep-Alive");

    reportPath = Paths.get(localPath);

    if (Files.exists(reportPath)) {

        if (Files.isReadable(reportPath)) {

            reportByteArray = Files.readAllBytes(reportPath);

            report = new ByteArrayInputStream(reportByteArray);
        }
    }

    fileSize = report.available();

    responseBuilder.header("Content-Length", fileSize);

    baseResponseMessage.setPayload(report);

    return responseBuilder.build();
}

通过查看调试器,我确实知道正确找到了路径和 excel 文件,并且 fileSize 也正确填充。

我很乐意提供所需的更多信息。感谢您的宝贵时间!

【问题讨论】:

    标签: java excel rest websphere-8 apache-wink


    【解决方案1】:

    我认为问题出在@Produces 注释上。默认情况下,JAX-RS 可能不知道如何处理"application/vnd.ms-excel"。您可以尝试使用

    @Produces( MediaType.APPLICATION_OCTET_STREAM)
    

    这里是示例代码,

    1. 创建一个 JEE 6 网络项目

    2. 创建了一个应用类

      import javax.ws.rs.ApplicationPath;  
      import javax.ws.rs.core.Application; 
      
      @ApplicationPath("/services/*")  
      public class MyApplication extends Application {  
      
      }  
      
    3. 创建了一个 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.MediaType;  
      import javax.ws.rs.core.Response;  
      import javax.ws.rs.core.Response.ResponseBuilder;  
      
      
      @Path("/hello")  
      public class HelloResource {  
      
          @GET  
          @Path("excel")  
          @Produces(MediaType.APPLICATION_OCTET_STREAM)  
          public Response test2() {  
              File file = new File("C:/users/arun/rest-test.xlsx");  
              ResponseBuilder rb = Response.ok(file);  
              rb.header("content-disposition", "attachment; filename=rest-test.xlsx");  
              return rb.build();  
          }  
      }  
      

    【讨论】:

    • 显然这不是问题,因为我在进行更改时收到此消息:org.apache.wink.server.internal.handlers.FlushResultHandler handleResponse The system could not find a javax.ws.rs.ext.MessageBodyWriter or a DataSourceProvider class for the com.somewhere.else.message.core.BaseResponseMessage type and application/octet-stream mediaType. Ensure that a javax.ws.rs.ext.MessageBodyWriter exists in the JAX-RS application for the type and media type specified. 也许它与在 BaseResponseMessage 而不是直接返回的响应有关?
    【解决方案2】:

    问题在于 BaseResponseMessage 的存在。显然是因为我实际上是在 Response 对象中返回 BaseResponseMessage 而不是实际的 InputStream JAX-RS 不知道如何处理它,因为(如错误所示)没有专门关联的 MessageBodyWriter 或 DataSourceProvider那个组合。有趣的是,如果我更仔细地阅读,错误消息实际上如何揭示了实际问题! :p

    当上面的代码被修改以删除 BaseResponseMessage 并执行以下操作时:

    responseBuilder = Response.ok(report);

    那么它就可以正常工作了。

    [顺便说一句,返回InputStream 并不是一个好主意;该代码已被修改为返回一个StreamingOutput 对象。]

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多