Date类型参数报错
在Spring cloud feign接口中传递Date类型参数时报错,报错信息。场景:
客户端传递一个new Date()的参数,服务端接受的参数和客户端有时间差。客户端打印格式化的new Date():2018-05-11 10:23:36
而服务端接收到的参数是:
2018-05-12 00:23:36
我们从Feign启动的源码可以看出,Feign在encode和decode时会用SpringEncoder类来实现:
@Bean @ConditionalOnMissingBean public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)); } @Bean @ConditionalOnMissingBean public Encoder feignEncoder() { return new SpringEncoder(this.messageConverters); }
而SpringEncoder的HttpMessageConverters使用的是Jackson默认模板,该模板来自基类WebMvcConfigurationSupport.java:
[image:FDEB2409-A536-465C-9229-B65B23F4EFA9-340-0000031DA8363321/4A0D3ACD-13DE-45C3-B7E3-3C7E0CF03A1D.png]protected final List> getMessageConverters() { if (this.messageConverters == null) { this.messageConverters = new ArrayList >(); configureMessageConverters(this.messageConverters); if (this.messageConverters.isEmpty()) { addDefaultHttpMessageConverters(this.messageConverters); } extendMessageConverters(this.messageConverters); } return this.messageConverters; }
而WebMvcConfigurationSupport.java最终使用的是默认的ObjectMapper生成的MappingJackson2HttpMessageConverter。至此可以看出该问题的产生并不是Feign的问题,而是Feign实现中使用的Spring MVC中的Jackson转换参数问题,默认的TimeZone并不是东八区,而是UTC。
/** * Override the default {@link TimeZone} to use for formatting. * Default value used is UTC (NOT local timezone). * @since 4.1.5 */ public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) { this.timeZone = timeZone; return this; }
这个问题,在Spring MVC中可以在接口或者字段上添加注解来解决,但在Feign中使用GET请求的接口添加注解是不行的。debug发现,Spring MVC在处理Date的时候,调用了sun.reflect.ConstructorAccessor#newInstance(Object[] var1),时间会加14个小时。具体实现没看到源码,后续再研究。需要说明的是,加JsonFormat注解对于Feign接口没生效,但Spring MVC是可以的。
OK,回到正题。要解决这个问题,最好的办法是自定义ObjectMapper。即使是加了注解可以解决问题,也依然推荐使用自定义ObjectMapper,因为大量的接口每个都添加注解太繁琐了。@Bean @Primary public ObjectMapper objectMapper() { return Jackson2ObjectMapperBuilder.json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .timeZone(TimeZone.getTimeZone("Asia/Shanghai")) .build(); }
这样注解进去的ObjectMapper就带了时区。
LocalDate类型报错
报错详情:
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@3ce2b1e2; line: 1, column: 44] (through reference chain: com.chunrun.user.param.UserParams["localDate"])
这是因为LocalDate没有提供默认的构造器,而Jackson还不支持Java8的特征。这时候只需要加上依赖,ObjectMapper加一行代码即可:
com.fasterxml.jackson.datatype jackson-datatype-jsr310 2.4.0
@Bean @Primary public ObjectMapper objectMapper() { return Jackson2ObjectMapperBuilder.json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .timeZone(TimeZone.getTimeZone("Asia/Shanghai")) .modules(new JSR310Module()) .build(); }
以上配置调用方也需要。
以上。