feign 序列化_Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题...

LocalDate、LocalTime、LocalDateTime是Java 8开始提供的时间日期API,主要用来优化Java 8以前对于时间日期的处理操作。然而,我们在使用Spring Boot或使用Spring Cloud Feign的时候,往往会发现使用请求参数或返回结果中有LocalDate、LocalTime、LocalDateTime的时候会发生各种问题。本文我们就来说说这种情况下出现的问题,以及如何解决。

问题现象

先来看看症状。比如下面的例子:

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@RestController

class HelloController {

@PostMapping("/user")

public UserDto user(@RequestBody UserDto userDto) throws Exception {

return userDto;

}

}

@Data

@NoArgsConstructor

@AllArgsConstructor

static class UserDto {

private String userName;

private LocalDate birthday;

}

}

上面的代码构建了一个简单的Spring Boot Web应用,它提供了一个提交用户信息的接口,用户信息中包含了LocalDate类型的数据。此时,如果我们使用Feign来调用这个接口的时候,会得到如下错误:

2018-03-13 09:22:58,445 WARN [http-nio-9988-exec-3] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

at [Source: java.io.PushbackInputStream@67064c65; line: 1, column: 63] (through reference chain: java.util.ArrayList[0]->com.didispace.UserDto["birthday"])

分析解决

对于上面的错误信息JSON parse error: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value,熟悉Spring MVC的童鞋应该马上就能定位错误与LocalDate的反序列化有关。但是,依然会有很多读者会被这段错误信息java.util.ArrayList[0]->com.didispace.UserDto["birthday"]所困惑。我们命名提交的UserDto["birthday"]是个LocalDate对象嘛,跟ArrayList列表对象有啥关系呢?

我们不妨通过postman等手工发一个请求看看服务端返回的是什么?比如你可以按下图发起一个请求:

从上图中我们就可以理解上面我所提到的困惑了,实际上默认情况下Spring MVC对于LocalDate序列化成了一个数组类型,而Feign在调用的时候,还是按照ArrayList来处理,所以自然无法反序列化为LocalDate对象了。

解决方法

为了解决上面的问题非常简单,因为jackson也为此提供了一整套的序列化方案,我们只需要在pom.xml中引入jackson-datatype-jsr310依赖,具体如下:

com.fasterxml.jackson.datatype

jackson-datatype-jsr310

注意:在设置了spring boot的parent的情况下不需要指定具体的版本,也不建议指定某个具体版本

在该模块中封装对Java 8的时间日期API序列化的实现,其具体实现在这个类中:com.fasterxml.jackson.datatype.jsr310.JavaTimeModule(注意:一些较早版本疯转在这个类中“com.fasterxml.jackson.datatype.jsr310.JSR310Module)。在配置了依赖之后,我们只需要在上面的应用主类中增加这个序列化模块,同时开启标准的ISO 8601格式:

@Bean

public ObjectMapper serializingObjectMapper() {

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

objectMapper.registerModule(new JavaTimeModule());

return objectMapper;

}

此时,我们在访问刚才的接口,就不再是数组类型了,同时对于Feign客户端的调用也不会再出现上面的错误了。

代码示例

本文的相关例子可以查看下面仓库中的Chapter3-1-7目录:

Spring Booot 2.0 新特性详解正在连载,点击看看都有哪些解读

要在Spring Cloud Feign配置FastJson序列化,您需要完成以下步骤: 1. 添加FastJson依赖 在项目的pom.xml文件,添加FastJson依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> ``` 2. 创建FastJson转换器 创建一个FastJson转换器类,继承自Spring的HttpMessageConverter接口,并实现其方法。代码如下: ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.serializer.SerializerFeature; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Type; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; public class FastJsonHttpMessageConverter implements HttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private SerializerFeature[] serializerFeatures = new SerializerFeature[0]; private Feature[] parserFeatures = new Feature[0]; @Override public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) { return true; } @Override public boolean canWrite(Type type, Class<?> aClass, MediaType mediaType) { return true; } @Override public List<MediaType> getSupportedMediaTypes() { List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); mediaTypes.add(MediaType.APPLICATION_JSON); mediaTypes.add(MediaType.TEXT_PLAIN); return mediaTypes; } @Override public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException { InputStream inputStream = inputMessage.getBody(); return JSON.parseObject(inputStream, DEFAULT_CHARSET, type, parserFeatures); } @Override public void write(Object o, Type type, MediaType mediaType, HttpOutputMessage outputMessage) throws IOException { HttpHeaders headers = outputMessage.getHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); String jsonString = JSON.toJSONString(o, serializerFeatures); OutputStream outputStream = outputMessage.getBody(); outputStream.write(jsonString.getBytes(DEFAULT_CHARSET)); outputStream.flush(); } public SerializerFeature[] getSerializerFeatures() { return serializerFeatures; } public void setSerializerFeatures(SerializerFeature[] serializerFeatures) { this.serializerFeatures = serializerFeatures; } public Feature[] getParserFeatures() { return parserFeatures; } public void setParserFeatures(Feature[] parserFeatures) { this.parserFeatures = parserFeatures; } } ``` 3. 配置FastJson转换器 在Spring的配置类,创建一个FastJson转换器的bean,并将其注册到Spring的HttpMessageConverters。代码如下: ```java import com.alibaba.fastjson.serializer.SerializerFeature; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.List; @Configuration public class FeignConfiguration { @Bean public HttpMessageConverter fastJsonHttpMessageConverter() { return new FastJsonHttpMessageConverter(); } @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(new ObjectMapper()); messageConverters.add(converter); messageConverters.add(fastJsonHttpMessageConverter()); restTemplate.setMessageConverters(messageConverters); return restTemplate; } } ``` 4. 配置Feign Client 在Feign Client的配置类使用@FeignClient注解的configuration属性,将FastJson转换器的bean引入到Feign Client。代码如下: ```java import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableFeignClients(defaultConfiguration = {FeignConfiguration.class}) public class FeignClientConfiguration { } ``` 完成以上步骤之后,您就可以在Spring Cloud Feign使用FastJson作为序列化工具了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值