fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

springboot添加FastJsonHttpMessageConverter后使用文件下载的接口会报错

Open asd123wto opened this issue 1 year ago • 1 comments

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.core.io.InputStreamResource] with preset Content-Type 'application/octet-stream']

asd123wto avatar Jul 31 '24 08:07 asd123wto

@asd123wto

FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
...
converter.setSupportedMediaTypes(...);

VictorZeng avatar Aug 09 '24 07:08 VictorZeng

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());
    }

QYG2297248353 avatar Jan 21 '25 06:01 QYG2297248353