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

今天导出excel报错

1
No converter for [class org.springframework.core.io.ByteArrayResource] with preset Content-Type 'application/vnd.ms-excel'

代码如下:

1
2
3
4
5
6
7
8
9
@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配置一下即可

1
2
3
4
5
6
7
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.setObjectMapper(JacksonUtil.newObjectMapper());
jackson2HttpMessageConverter.setSupportedMediaTypes(Lists.of(MediaType.ALL));
converters.add(jackson2HttpMessageConverter);
}