0

我正在开发一个控制器,该控制器为我的用户提供数据拉取的 CSV 下载,并且我有一个对象、控制器和 HttpMessageConverter 基于此处非常有用的帖子: https ://stackoverflow.com/a/9961748/ 613559

我的 CsvResponse 对象:

public class CsvResponse {    
    private final String filename;
    private final List<Object[]> records;
    private final List<String> columns;

    public CsvResponse(List<Object[]> records, List<String> columns, String filename) {
        this.records = records;
        this.filename = filename;
        this.columns = columns;
    }
    public String getFilename() {
        return filename;
    }
    public List<Object[]> getRecords() {
        return records;
    }
    public List<String> getColumns() {
        return columns;
    }
}

我的控制器:

@RequestMapping(value="/download", method = RequestMethod.POST, produces = "text/csv")
public @ResponseBody CsvResponse download (@RequestParam("jsonData") String jsonData) throws IOException {
    // populate dataset and columns, removed for simplicity

    return new CsvResponse(dataset, columns, "download_" + new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss").format(new Date()) + ".csv");
}

我的 HttpMessageConverter:

public class CsvMessageConverter extends AbstractHttpMessageConverter<CsvResponse> {
    public static final MediaType MEDIA_TYPE = new MediaType("text", "csv", Charset.forName("utf-8"));
    public CsvMessageConverter() {
        super(MEDIA_TYPE);
    }

    protected boolean supports(Class<?> clazz) {
        return CsvResponse.class.equals(clazz);
    }

    protected void writeInternal(CsvResponse response, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException {
        OutputStream out;
        CsvWriter writer;
        List<Object[]> dataset;

        output.getHeaders().setContentType(MEDIA_TYPE);
        output.getHeaders().set("Content-Disposition", "attachment; filename=\"" + response.getFilename() + "\"");
        out = output.getBody();
        writer = new CsvWriter(new OutputStreamWriter(out), '\u0009');
        dataset = response.getRecords();
        for(Object[] datasetRow : dataset) {
            for(Object cell : datasetRow) {
                writer.write((cell instanceof String) ? "\"" + cell.toString() + "\"" : cell.toString());
            }
            writer.endRecord();
        }
        writer.flush();
        writer.close();
    }

    @Override
    protected CsvResponse readInternal(Class<? extends CsvResponse> type, HttpInputMessage him) throws IOException, HttpMessageNotReadableException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

当我访问下载控制器时,我收到错误消息org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation。调试显示CsvMessageConverter没有被访问。如果我produces = "text/csv"从控制器中删除该段,我会得到 CsvResponse 对象的 JSON 字符串表示形式(对于 @ResponseBody 注释来说是正确的),因此其余代码可以正常工作。我想我只是在这里遗漏了一些非常简单的东西,但是在将头撞到墙上一整天之后,我仍然看不到它。我需要更改什么才能让服务器使用CsvMessageConverter

4

1 回答 1

1

您应该通过将此块添加到 spring-mvc.xml 来注册您的 csv 转换器

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="com.xxx.utils.CsvMessageConverter"></bean>
    </mvc:message-converters>
</mvc:annotation-driven>

我写了一个例子,它可以工作。

于 2013-08-31T03:18:14.853 回答