In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "spring-cloud-feign how to serialize Java8 dates". In daily operation, I believe many people have doubts about how spring-cloud-feign serializes Java8 dates. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how spring-cloud-feign serializes Java8 dates". Next, please follow the editor to study!
Spring-cloud-feign 's preface to serialization of Java8 dates
If you have used spring-cloud, you should know that feign interface defaults to http to make requests. Generally, web projects of spring are serialized and deserialized through Jackson. During object transfer, if the time type in the object is Java8's time class, deserialization may fail. Therefore, we need to configure a custom serialization for the time class provided by Java8. We use the LocalDateTime example by default, and LocalDate and LocalTime are actually the same.
Introduce JSR310 dependency
Within the project, we refer to the dependency
Com.fasterxml.jackson.datatype jackson-datatype-jsr310 2.8.11 uses JSR310 default configuration
By default, JSR310 provides us with serialization / deserialization classes that are converted to strings, which are LocalDateTimeSerializer/LocalDateTimeDeserializer, which we can serialize using the existing implementation.
Then we return a Bean through the automatic assembly of Spring
@ Configurationpublic class JacksonSerializerConfiguration {@ Bean public ObjectMapper buildObjectMapper () {ObjectMapper objectMapper = new ObjectMapper (); JavaTimeModule javaTimeModule = new JavaTimeModule (); javaTimeModule.addSerializer (LocalDateTime.class,new LocalDateTimeSerializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss")) .addroomializer (LocalDateTime.class,new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss") ObjectMapper.registerModule (javaTimeModule); return objectMapper;}} use custom configuration
Of course, we don't want to use the default, we can customize the configuration, inherit the JsonSerializer class, implement abstract methods, or inherit JSR310's InstantSerializer class, and then rewrite the constructor. As an example, we inherit JsonSerializer as a demonstration.
For example, we want to serialize the timestamp of LocalDateTime to seconds, with the following code
Public class InstantSerializer extends JsonSerializer {@ Override public void serialize (LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {gen.writeNumber (DateUtils.toSecond (value));}} public class InstantDeserializer extends JsonDeserializer {@ Override public LocalDateTime deserialize (JsonParser p, DeserializationContext txt) throws IOException, JsonProcessingException {return Instant.ofEpochSecond (p.getLongValue ()) .atZone (ZoneId.systemDefault ()). ToLocalDateTime () } @ Configurationpublic class JacksonSerializerConfiguration {@ Bean public ObjectMapper buildObjectMapper () {ObjectMapper objectMapper = new ObjectMapper (); JavaTimeModule javaTimeModule = new JavaTimeModule (); javaTimeModule.addSerializer (LocalDateTime.class,new InstantSerializer ()) .addaccounializer (LocalDateTime.class,new InstantDeserializer ()); objectMapper.registerModule (javaTimeModule); return objectMapper;}} Summary
JSR310 gives us a lot of basic implementation, simple can use the basic implementation, if you want to customize, through inheriting the abstract class provided by jackson, implement by yourself, the two are similar in implementation, but through inheritance more flexible, more controllable, more convenient to use jsr310, the specific use of different people have different opinions
At this point, the study on "how spring-cloud-feign serializes Java8 dates" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.