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

查看:406
本文介绍了找不到针对媒体类型=应用程序/八位字节流,类型=类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,类型为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"),但是遇到了类似的错误.有人对我如何获得它以返回电子表格提出建议吗?上一次我收到与此类似的错误消息(表示无法为arraylist找到消息正文编写器),我只是将其包装在一个通用实体中.这次不起作用了.

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实体提供程序中进一步了解它们.

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天全站免登陆