找不到媒体类型 = 应用程序/八位字节流,类型 = 类 org.apache.poi.xssf.usermodel.XSSFWorkbook 的 MessageBodyWriter [英] MessageBodyWriter not found for media type=application/octet-stream, type=class org.apache.poi.xssf.usermodel.XSSFWorkbook

查看:31
本文介绍了找不到媒体类型 = 应用程序/八位字节流,类型 = 类 org.apache.poi.xssf.usermodel.XSSFWorkbook 的 MessageBodyWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类,它试图以 excel 电子表格的形式返回一些数据.我收到了错误

I have the below class that tries to return some data in the form of an excel spreadsheet. I'm getting the error

找不到媒体类型=application/octet-stream, type=class org.apache.poi.xssf.usermodel.XSSFWorkbook 的MessageBodyWriter

MessageBodyWriter not found for media type=application/octet-stream, type=class org.apache.poi.xssf.usermodel.XSSFWorkbook

我也尝试过 @Produces("application/vnd.ms-excel"),但也遇到了类似的错误.有人对我如何让这个返回电子表格有建议吗?上次我收到与此类似的错误消息(抱怨找不到数组列表的消息正文编写器)时,我只是将它包装在一个通用实体中.这一次没有奏效.

I've also tried @Produces("application/vnd.ms-excel"), but have gotten similar errors. Anyone have a suggestion as to how I can get this to return a spreadsheet? The last time I got an error message similar to this (complaining that a message body writer couldn't be found for arraylist) I just wrapped it in a generic entity. That trick didn't work this time.

@PermitAll
@Path("uploadWorkbook")
public class ExcelUploadResource {

    @Context
    ResourceContext resourceContext;

    @Inject
    JobService jobService;

    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response list() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Job definitions");

        int rowNum = 0;
        for(Job job : jobService.list()){
            Row row = sheet.createRow(rowNum++);
            int cellNum = 0;
            for(String field : job.toList()){
                Cell cell = row.createCell(cellNum++);
                cell.setCellValue(field);
            }
        }

        GenericEntity<XSSFWorkbook> entity = new GenericEntity<XSSFWorkbook>(workbook) {};

        ResponseBuilder response = Response.ok(entity);
        response.header("Content-Disposition",
            "attachment; filename=jobs.xls");
        return response.build();
    }   
}

推荐答案

您不能只使用数据类型为 application/octet-stream 的任意对象.您首先需要了解的是对象是如何序列化的.这是通过使用 MessageBodyWriter 完成的.您可以在 JAX-RS Entity Providers 中了解有关它们的更多信息.

You can't just use arbitrary objects with the data type application/octet-stream. What you first need to understand is how objects are serialized. This is done with the use of MessageBodyWriters. You can learn more about them in JAX-RS Entity Providers.

编写器的工作方式是传递实体和响应流.编写器应该获取实体并将实体的内容写入响应流.根据我们返回的实体类型和预期的媒体类型查找作者,在您的情况下,您希望它是 application/octet-stream.

How the writer works is that it is passed the entity and the response stream. The writer is supposed to take the entity and write the contents of the entity to the response stream. The writers are looked up by the type of entity we return and the media type expected, in your case you want it to be application/octet-stream.

错误的意思是没有编写器来处理 XSSFWorkbook 的转换.当您谈论 application/octet-stream 时,您主要是在处理二进制文件.XSSFWorkbook 不是二进制文件.使用 application/octet-stream 时,您将主要使用 byte[]FileInputStreamStreamingOutput 实体类型.因此,如果您想使用 application/octet-stream,则需要将实体更改为其中一种类型.

What the error is saying is that there is no writer to handle the conversion of your XSSFWorkbook. When you talk about application/octet-stream, you're mostly dealing with binary files. XSSFWorkbook is not a binary file. When working with application/octet-stream, you'll mostly be working with byte[], File, InputStream, and StreamingOutput entity types. So if you want to use application/octet-stream, then you would need to change the entity to be one of those types.

我从未使用过 Apache POI,但只是通过一个快速教程,看起来你可能想要在这种情况下使用的是 StreamingOutput,你可以只使用 XSSFWorkbook#write(OutputStream) 方法将工作簿写入 StreamingOutput

I've never used Apache POI, but just going through a quick tutorial, it looks like what you probably want to use for this case is the StreamingOutput, you can just use the XSSFWorkbook#write(OutputStream) method to write the workbook to the StreamingOutput

public Response getExcelFile() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    ...
    StreamingOutput output = new StreamingOutput() {
        @Override
        public void write(OutputStream out)
                throws IOException, WebApplicationException {

            workbook.write(out);
            out.flush();
        }
    };
    return Response.ok(output)
            .header(HttpHeaders.CONTENT_DISPOSITION,
                    "attachment; filename=jobs.xls")
            .build();
}

这篇关于找不到媒体类型 = 应用程序/八位字节流,类型 = 类 org.apache.poi.xssf.usermodel.XSSFWorkbook 的 MessageBodyWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆