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 ...
随机推荐
- graphql-yoga 项目简单使用&&集成docker
graphql-yoga 是一个实现了grahql的框架,使用简单,便捷 具体源码参考github https://github.com/rongfengliang/graphql-yoga-do ...
- R学习笔记 ---- 系列文章
R实战 开篇:介绍R的使用 R学习笔记 第五篇:字符串操作 R学习笔记 第六篇:数据变换和清理 R学习笔记 第四篇:函数,分支和循环 R学习笔记 第三篇:数据框 R学习笔记 第二篇:矩阵.数组和列表 ...
- cocos2dx字体描边
LabelTTF::create(); 这样fontname那不填表示使用设备默认字体 std::string lvstr = FunctionUtil::getChinese("guank ...
- 【转】让开发变得简单一点- Visual Studio 2010几个让人印象深刻的新功能
原文网址:http://xhinker.blog.51cto.com/640011/313055/ 引言 "我们的目标,不仅仅是做出几个新功能,而是要回答一个问题:'如何让现在的开发人员生活 ...
- 【转】详解硬盘MBR
原文网址:http://hi.baidu.com/waybq/item/a4490f026f9859d21ef046a4 硬盘是现在计算机上最常用的存储器之一.我们都知道,计算机之所以神奇,是因为它具 ...
- windows dos权限管理
显示或者修改文件的访问控制列表(ACL) CACLS filename [/T] [/M] [/L] [/S[:SDDL]] [/E] [/C] [/G user:perm] ...
- CentOS 下tomcat安装
1. 下载tomcat, http://apache.fayea.com/tomcat/tomcat-8/v8.5.16/bin/apache-tomcat-8.5.16.tar.gz 我下载的是这个 ...
- java编码-多重(正常)
String ISO = "ISO-8859-1"; String UTF = "UTF-8"; String GBK = "GBK"; S ...
- java8时间使用小结
//LocalDate代表一个IOS格式(yyyy-MM-dd)的日期 获取当前的日期: LocalDate localDate = LocalDate.now();//LocalDate: 表示没有 ...
- Sql Server-使用Sql Server自带的分词功能实现字段关键词提取(分词能力很低,慎用)
“创建全文索引 启动服务 在SQL Server配置管理工具中,找到'SQL Full-text Filter Daemon Launcher'服务用本地用户启动. 创建全文目录 打开需要创建全文目录 ...