解决 ‘Content-Type‘ cannot contain wildcard type ‘*‘异常和JSON parse error: Cannot deserialize value of

本文记录了在使用Feign进行远程调用时遇到的时间格式转换问题及解决过程。主要涉及日期格式不匹配导致的解析错误和Content-Type配置不当引发的编码异常。

feign调用中遇到的问题
记:今天开发中遇到feign调用时,遇到时间转换的问题
1、status":400,“error”:“Bad Request”,“message”:"JSON parse error: Cannot deserialize value of type java.util.Date from String “2021-08-03 14:04:53”
2、feign.codec.EncodeException: Content-Type cannot contain wildcard type '*'错误

{“timestamp”:“2021-08-03T06:04:53.342+0000”,“status”:400,“error”:“Bad Request”,“message”:"JSON parse error: Cannot deserialize value of type java.util.Date from String “2021-08-03 14:04:53”: not a valid representation (error: Failed to parse Date value ‘2021-08-03 14:04:53’: Cannot parse date “2021-08-03 14:04:53”: while it seems to fit format ‘yyyy-MM-dd’T’HH:mm:ss.SSSZ’, parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.util.Date from String “2021-08-03 14:04:53”: not a valid representation (error: Failed to parse Date value ‘2021-08-03 14:04:53’: Cannot parse date “2021-08-03 14:04:53”: while it seems to fit format ‘yyyy-MM-dd’T’HH:mm:ss.SSSZ’, parsing fails (leniency? null))\n at [Source: (PushbackInputStream); line: 1, column: 39] (through reference chain: java.util.ArrayList[0]->com.aa.bb.user.UserDO[“createTime”])

com.netflix.hystrix.AbstractCommand[0;39m Error executing HystrixCommand.run(). Proceeding to fallback logic …
feign.FeignException$BadRequest: status 400 reading UserService#save(List)
报错。
查阅了网上的资料,发现说需要在实体类UserDO的createTime中添加注解
@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
@DateTimeFormat(“yyyy-MM-dd HH:mm:ss”)
添加后,发现还是不行,然后新写一个配置类,实现WebMvcConfigurer接口。

解决方案:
1、在实体类上添加注解
@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
@DateTimeFormat(“yyyy-MM-dd HH:mm:ss”)
2、新建一个配置类,如下:

public class WebMvcConfigurerImpl implements WebMvcConfigurer {
	@Bean
	public HttpMessageConverters fastjsonHttpMessageConverter() {
		// 定义一个转换消息的对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

		// 添加fastjson的配置信息 比如 :是否要格式化返回的json数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();

		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

		// 在转换器中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);

		// 如果不添加下面的配置,会报feign.codec.EncodeException: Content-Type cannot contain
		// wildcard type '*'错误
		List<MediaType> supportedMediaTypes = new ArrayList<>(23);
		supportedMediaTypes.add(MediaType.APPLICATION_JSON);
		supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
		supportedMediaTypes.add(MediaType.APPLICATION_CBOR);
		supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
		supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
		supportedMediaTypes.add(MediaType.APPLICATION_PDF);
		supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON);
		supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_XML);
		supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
		supportedMediaTypes.add(MediaType.APPLICATION_STREAM_JSON);
		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.MULTIPART_FORM_DATA);
		supportedMediaTypes.add(MediaType.MULTIPART_MIXED);
		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);

		HttpMessageConverter<?> converter = fastConverter;

		return new HttpMessageConverters(converter);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值