Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'
升级到最新版本的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);
}
}
Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'的更多相关文章
- spring boot 启动遇到报错:Failed to configure a DataSource
spring boot 启动遇到报错,具体如下 Description: Failed to configure a DataSource: 'url' attribute is not speci ...
- spring boot中连接数据库报错500(mybatis)
spring boot中连接数据库报错500(mybatis) pom.xml中的依赖 <!-- 集成mybatis--> <dependency> <groupId&g ...
- Spring boot临时文件目录报错
基本的错误信息如下: 2018-03-05 at 15:12:03 CST ERROR org.apache.juli.logging.DirectJDKLog 181 log - Servlet.s ...
- Spring Boot整合Mybatis报错InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider
Spring Boot整合Mybatis时一直报错 后来发现原来主配置类上的MapperScan导错了包 由于我使用了通用Mapper,所以应该导入通用mapper这个包
- Spring Boot整合Swagger报错:"this.condition" is null
前段时间看到群里有吐槽swagger整合问题,当时没仔细看,总以为是姿势不对. 这两天正好自己升级Spring Boot版本,然后突然出现了这样的一个错误: Caused by: java.lang. ...
- 创建spring boot项目启动报错遇到的问题
1.Spring boot,Mybatis 启动报错 Failed to auto-configure a DataSource *************************** APPLICA ...
- Spring boot配置fastjson
pom 文件引用 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...
- spring boot 整合kafka 报错 Exception thrown when sending a message with key='null' and payload=JSON to topic proccess_trading_end: TimeoutException: Failed to update metadata after 60000 ms.
org.springframework.kafka.support.LoggingProducerListener- Exception thrown when sending a message w ...
- 【spring boot Mybatis】报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.interview.dao.UserMapper.add
报错如下: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.i ...
随机推荐
- Python :random 随机数生成
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random() 用于生成一个0到1的随机符点数: 0 &l ...
- Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] }的差别
Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] } Array.new(size, default_object) creates an arr ...
- FastAdmin bootstrap-table 分页手动输入跳转
FastAdmin bootstrap-table 分页手动输入跳转 Bootstrap-Table (V1.11.0)默认是没有这个功能的,不过作者有写的扩展. https://github.com ...
- PostgreSQL 9.6 keepalived主从部署
## 环境: PostgreSQL版:9.6 角色 OS IPmaster CentOS7 ...
- zipkin:mysql做存储,kafka做接收器,以及如何找到配置名称
mysql设定 1. 创建表结构: (源码路径)\zipkin-storage\mysql\src\main\resources\mysql.sql 2. zipkin的存储设置为mysql(coll ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- jetty之嵌入式开发
一.Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可 ...
- UI“三重天”之Selenium(一)
关注一下UI自动化,记一记笔记. UI自动化的优缺点: 关于UI自动化的优缺点想来大家都有了解,优点:解放人力(并不是完全解放),用机器(涵盖工具.脚本等)代替人工完成测试工作,将测试用例转化为脚本实 ...
- Golang基础学习总结
转自:http://blog.csdn.net/yue7603835/article/details/44264925 1.不支持继承.重载 ,比如C++.Java的接口,接口的修改会影响整个实现改接 ...
- pycharm -- 小技巧1 (显示文件的代码结构以及错误提示)
背景介绍 今天上午,在调用同事昨天给的算法程序时出了点问题,于是请同事来我这边一起调代码.大致场景描述如下: 我:B神,你昨天下班前给我的那个算法程序我这边调用的时候出现错误啦,请你过来看下呗. 同事 ...