mvc导出excel报错No converter

2023-06-23 11:02:35 浏览数 (2)

无翼而飞者,声也。——佚名

今天导出excel报错

代码语言:javascript复制
No converter for [class org.springframework.core.io.ByteArrayResource] with preset Content-Type 'application/vnd.ms-excel'

代码如下:

代码语言:javascript复制
@PostMapping("exportSettle")
fun export(dto: CommonDTO) {
	val writer = ExcelUtil.getWriter()
	writer.writeRow(Maps.of("key", "value"), true)
	response.addHeader("Content-Disposition", writer.getDisposition("test.xlsx", CharsetUtil.CHARSET_UTF_8))
	response.contentType = ContentType.build(writer.contentType, StandardCharsets.UTF_8)
	writer.flush(response.outputStream, true)
	writer.close()
}

发现没有配置MediaTypes

MvcConfig配置一下即可

代码语言:javascript复制
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
	jackson2HttpMessageConverter.setObjectMapper(JacksonUtil.newObjectMapper());
	jackson2HttpMessageConverter.setSupportedMediaTypes(Lists.of(MediaType.ALL));
	converters.add(jackson2HttpMessageConverter);
}

0 人点赞