当前位置:网站首页>Feign request return value inverse sequence localdatetime exception record
Feign request return value inverse sequence localdatetime exception record
2022-06-24 06:02:00 【lyb-geek】
Preface
Used by recent project team feign Call remote service , The consumer reported the following exception
From the abnormal information, we can get localdatime Deserialized exception , And this anomaly is because jackson Failure to process results in . So we can do it for jackson Of ObjectMapper Fit it
resolvent
1、 stay pom.xml introduce
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>notes : jackson-datatype-jsr310 This is used to support jsr310 Standard time ,jackson-datatype-jdk8 Used to support new specific JDK8 The type of , for example Optional
2、 Replace the default ObjectMapper
@Configuration
public class LocalDateTimeConfig {
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT));
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());
return objectMapper;
}Questions : Why replace the default ObjectMapper after ,feign You can deal with LocalDateTime
The answer lies in
@Configuration(proxyBeanMethods = false)
public class FeignClientsConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new OptionalDecoder(
new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
}
}and messageConverters The default converter is based on HttpMessageConvertersAutoConfiguration And come
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(HttpMessageConverter.class)
@Conditional(NotReactiveWebApplicationCondition.class)
@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class, JsonbAutoConfiguration.class })
@Import({ JacksonHttpMessageConvertersConfiguration.class, GsonHttpMessageConvertersConfiguration.class,
JsonbHttpMessageConvertersConfiguration.class })
public class HttpMessageConvertersAutoConfiguration {
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
}ObjectProvider It has the function of delayed loading , It will be loaded according to the actual situation .springboot Of web The module will introduce... By default Jackson Related packages . There is a passage on the official website
This indicates the default HttpMessageConverter by MappingJackson2HttpMessageConverter, and MappingJackson2HttpMessageConverter Make use of objectMapper To serialize and deserialize
The above words are sorted in code as follows
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
@Bean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(messageConverters()));
}
public ObjectFactory<HttpMessageConverters> messageConverters() {
return () -> new HttpMessageConverters(mappingJackson2HttpMessageConverter());
}
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
return new MappingJackson2HttpMessageConverter(objectMapper());
}
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT));
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());
return objectMapper;
}summary
Exception information is important , Source code is very important , The official website is equally important
边栏推荐
- Fixed assets management software enables enterprises to realize intelligent management of fixed assets
- How to resolve the domain name? How to choose a domain name?
- Spirit information development log (4)
- How to check whether the domain name is filed? Must the domain name be filed for use?
- How to get a secondary domain name? What does a secondary domain name mean?
- Script updates CLB type ingress Certificate in tke cluster
- Playing "honey in snow and ice city" with single chip microcomputer
- Several relations to be clarified in the process of digital transformation: stock and increment
- What is the difference between a white box test and a black box test
- How to file a personal domain name? What are the benefits of domain name filing?
猜你喜欢
随机推荐
Kubernetes Chapter 1: Foundation
New tea: reshuffle, transformation, merger and acquisition
Clickhouse alter table execution process
What are the domain name registration query tools? What should be paid attention to when registering a domain name
How do virtual hosts bind domain names? Can binding failure be used normally?
Ups and esxi realize automatic shutdown after power failure
Figure 1 understand Tencent reassurance platform
What is the difference between a white box test and a black box test
How to renew the domain name when it expires
How to buy a domain name? How to do a good job in website construction?
Fixed assets management software enables enterprises to realize intelligent management of fixed assets
Precautions for selecting high frequency signal generator
How to register the company domain name mailbox? Is the operation process complicated
How to do reverse domain name resolution? What does reverse domain name resolution mean?
Go concurrency - work pool mode
Tomorrow, we will help farmers make their debut together!
Risc-v assembly language programming (2) assembly program ASM_ run_ led
Several relations to be clarified in the process of digital transformation: stock and increment
Analysis of DDoS attack methods
The joint network security laboratory of runlian technology and Tencent security was officially unveiled


