gpt4 book ai didi

java - Spring MVC错误: org. springframework.web.HttpMediaTypeNotAcceptableException

转载 作者:行者123 更新时间:2023-12-01 14:10:47 24 4
gpt4 key购买 nike

我正在开发一个 Controller ,为我的用户提供数据拉取的 CSV 下载,并且我有一个对象、 Controller 和 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;
}
}

我的 Controller :

@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.
}
}

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

最佳答案

您应该通过将此 block 添加到 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>

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

关于java - Spring MVC错误: org. springframework.web.HttpMediaTypeNotAcceptableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18542614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com