关于springboot2使用@JsonFormat注解不生效的问题

百度了不少解决方式,有让用@JsonField,也有让把fastjson去掉的,也有加很多配置的,但是都没用。

本次使用的版本号:

1、springboot2.2.2 ;

2、fastjson 1.1.26

3、1.9.10
        2.10.3

第三点以及相关的依赖可能不需要,加上也没关系

pom文件中的依赖:


			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}
		

解决方式:

第一步新建一个转换类 用于自定义Jackson反序列化日期类型时应用的类型转换器,一般用于@RequestBody接受参数时使用

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;

/**
 * 自定义Jackson反序列化日期类型时应用的类型转换器,一般用于@RequestBody接受参数时使用

 *  次类是借用网上别的人的,增加了一个日期格式化的类型
 */
public class DateJacksonConverter extends JsonDeserializer {
    //此处尤为重要,请查找自己控制台报错的日期格式化类型是啥样的
    //我的是2020-04-29T16:23:44.999Z 所以我在下面的数组中添加了 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 格式;如果下面数组中没有你要的格式化类型,其他的可自行根据自己的情况去添加即可
    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;
    }
}

创建好上边的类之后,如果不想继续,则可以在实体对象属性上加注解就完全可以了。

导入的包路径为:import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 

@JsonDeserialize(using=DateJacksonConverter.class)
private Date startDate;

如果不想去让实体类加这个注解,可以在写一个公共配置,如下: 

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;

@Configuration
public 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;
    }
}

此时就不必再每个实体类属性上加注解了。

………………………………………………………………………………………………………………

你可能感兴趣的:(java,springboot)