In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to solve the problem that the @ JsonFormat annotation does not take effect in springboot2", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve the problem that the use of @ JsonFormat annotation in springboot2 is not effective".
Using @ JsonFormat annotations does not take effect
Baidu has a lot of solutions, including using @ JsonField, getting rid of fastjson, and adding a lot of configurations, but they are all useless.
The version number used this time
1 、 springboot2.2.2
2 、 fastjson 1.1.26
3 、 1.9.10
2.10.3
The third point and related dependencies may not be needed, and it doesn't matter if you add them.
Dependencies in the pom file:
Com.alibaba fastjson ${fastjson.version} org.codehaus.jackson jackson-mapper-asl ${jackson-mapper-asl.version} com.fasterxml.jackson.core jackson-core ${jackson-core.version} com.fasterxml. Jackson.core jackson-databind ${jackson-core.version} com.fasterxml.jackson.core jackson-annotations ${jackson-core.version} solution
The first step is to create a new conversion class to customize the type converter applied when Jackson deserializes the date type, which is generally used when @ RequestBody accepts parameters.
Import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.DeserializationContext;import com.fasterxml.jackson.databind.JsonDeserializer;import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.time.DateUtils; import java.io.IOException;import java.text.ParseException;import java.util.Date / * * Custom Jackson type converter used when deserializing date types is generally used when @ RequestBody accepts parameters * subclasses are borrowed from others on the Internet, and a date formatting type is added * / public class DateJacksonConverter extends JsonDeserializer {/ / is particularly important here Please find out what the date format type of error reported in your console is / / mine is 2020-04-29T16:23:44.999Z, so I added the format "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" to the following array. If the format type you want is not found in the following array Others can be added according to their own circumstances private static String [] pattern = new String [] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S", "yyyy.MM.dd", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH:mm:ss" "yyyy.MM.dd HH:mm:ss.S", "yyyy/MM/dd", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss.S", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"} @ Override public Date deserialize (JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {Date targetDate = null; String originDate = p.getText (); if (StringUtils.isNotEmpty (originDate)) {try {long longDate = Long.valueOf (originDate.trim ()); targetDate = new Date (longDate) } catch (NumberFormatException e) {try {targetDate = DateUtils.parseDate (originDate, DateJacksonConverter.pattern) } catch (ParseException pe) {throw new IOException (String.format ("% s' can not convert to type 'java.util.Date',just support timestamp (type of long) and following date format (% s)", originDate, StringUtils.join (pattern, ") } return targetDate;} @ Override public Class handledType () {return Date.class;}}
After creating the above class, if you don't want to continue, you can annotate the entity object properties.
The imported package path is:
Import com.fasterxml.jackson.databind.annotation.JsonDeserialize;@JsonDeserialize (using=DateJacksonConverter.class) private Date startDate
If you don't want to add this annotation to the entity class, you can write a public configuration as follows:
Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import com.fasterxml.jackson.databind.ObjectMapper;import com.fx.base.serializer.DateJacksonConverter; @ Configurationpublic class ConverterConfig {@ Bean public DateJacksonConverter dateJacksonConverter () {return new DateJacksonConverter () } @ Bean public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean (@ Autowired DateJacksonConverter dateJacksonConverter) {Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean (); jackson2ObjectMapperFactoryBean.setDeserializers (dateJacksonConverter); return jackson2ObjectMapperFactoryBean;} @ Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter (@ Autowired ObjectMapper objectMapper) {MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter (); mappingJackson2HttpMessageConverter.setObjectMapper (objectMapper) Return mappingJackson2HttpMessageConverter;}}
There is no need to annotate each entity class attribute at this point.
The role of @ JsonFormat and @ DateTimeFormat
@ DatetimeFormat is used to convert String to Date, which is usually used when passing values from the foreground to the background
When import org.springframework.format.annotation.DateTimeFormat; / * is sent to the background in the foreground, the string is automatically encapsulated into a date * / @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") private Date birth
@ JsonFormat (pattern= "yyyy-MM-dd") converts Date to String when passing values to the foreground in the background
When the import com.fasterxml.jackson.annotation.JsonFormat; / * background returns to the foreground, the date is automatically formatted * / @ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss") private Date birth
Note:
@ JsonFormat can not only complete the type conversion of background-to-foreground parameter passing, but also achieve foreground-to-background type conversion.
When content-type is application/json, the pattern of @ JsonFormat is preferred for type conversion. Instead of using @ DateTimeFormat for type conversion.
The purpose of the @ JsonFormat annotation is to convert json strings to java objects, regardless of the direction in which the parameters are passed.
The above is about the content of the article "how to solve the problem that the @ JsonFormat annotation in springboot2 does not work". I hope you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.