zoukankan      html  css  js  c++  java
  • Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'

    https://www.cnblogs.com/xiaopotian/p/8654993.html

    升级到最新版本的fastjson以后报的错,查了一下资料,发现

    fastjson从1.1.41升级到1.2.28之后,请求报错:
    json java.lang.IllegalArgumentException: 'Content-Type' cannot contain wildcard type '*'

    原因是在1.1.41中,FastJsonHttpMessageConverter初始化时,设置了MediaType。

        public FastJsonHttpMessageConverter(){
            super(new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8));
        }

    而在1.2.28中,设置的MediaType为‘/’,即:

        public FastJsonHttpMessageConverter() {
            super(MediaType.ALL);  // */*
        }

    后续在org.springframework.http.converter.AbstractHttpMessageConverter.write过程中,又要判断Content-Type不能含有通配符,这应该是一种保护机制,并强制用户自己配置MediaType。

    解决方案如下:

    在FastJson配置类中手动设置Content-Type

    复制代码
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * ==========================================
     * Created with IntelliJ IDEA.
     * User: 小破天
     * Date: 2018-03-26
     * Time: 23:52
     * 博客园:http://www.cnblogs.com/xiaopotian/
     * ===========================================
     */
    @Configuration
    public class FastJsonConfiguration extends WebMvcConfigurationSupport
    {
        /**
         * 修改自定义消息转换器
         * @param converters 消息转换器列表
         */
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            //调用父类的配置
            super.configureMessageConverters(converters);
            //创建fastJson消息转换器
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    
            //升级最新版本需加=============================================================
            List<MediaType> supportedMediaTypes = new ArrayList<>();
            supportedMediaTypes.add(MediaType.APPLICATION_JSON);
            supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
            supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
            supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
            supportedMediaTypes.add(MediaType.APPLICATION_PDF);
            supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
            supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
            supportedMediaTypes.add(MediaType.APPLICATION_XML);
            supportedMediaTypes.add(MediaType.IMAGE_GIF);
            supportedMediaTypes.add(MediaType.IMAGE_JPEG);
            supportedMediaTypes.add(MediaType.IMAGE_PNG);
            supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
            supportedMediaTypes.add(MediaType.TEXT_HTML);
            supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
            supportedMediaTypes.add(MediaType.TEXT_PLAIN);
            supportedMediaTypes.add(MediaType.TEXT_XML);
            fastConverter.setSupportedMediaTypes(supportedMediaTypes);
    
            //创建配置类
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            //修改配置返回内容的过滤
            //WriteNullListAsEmpty  :List字段如果为null,输出为[],而非null
            //WriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非null
            //DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
            //WriteNullBooleanAsFalse:Boolean字段如果为null,输出为false,而非null
            //WriteMapNullValue:是否输出值为null的字段,默认为false
            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteMapNullValue
            );
            fastConverter.setFastJsonConfig(fastJsonConfig);
            //将fastjson添加到视图消息转换器列表内
            converters.add(fastConverter);
        }
    }
    复制代码
  • 相关阅读:
    取消PHPCMS V9后台新版本升级提示信息
    phpcmsv9全站搜索,不限模型
    jq瀑布流代码
    phpcms v9模版调用代码
    angular.js添加自定义服务依赖项方法
    angular多页面切换传递参数
    angular路由最基本的实例---简单易懂
    作用域事件传播
    利用angular控制元素的显示和隐藏
    利用angular给节点添加样式
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/12657850.html
Copyright © 2011-2022 走看看