fastjson2
fastjson2 copied to clipboard
springboot添加FastJsonHttpMessageConverter后使用文件下载的接口会报错
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.core.io.InputStreamResource] with preset Content-Type 'application/octet-stream']
@asd123wto
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
...
converter.setSupportedMediaTypes(...);
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.core.io.InputStreamResource] with preset Content-Type 'application/octet-stream']
二进制文件,建议配置
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 默认消息转换器
converters.add(new MappingJackson2HttpMessageConverter());
// JSON 消息转换器
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(0, converter);
// 字节消息转换器
converters.add(new ByteArrayHttpMessageConverter());
// XML 消息转换器
converters.add(new Jaxb2RootElementHttpMessageConverter());
}