springboot2中使用@JsonFormat注解不生效的解決
使用@JsonFormat注解不生效
百度了不少解決方式,有讓用@JsonField,也有讓把fastjson去掉的,也有加很多配置的,但是都沒用。
本次使用的版本號(hào)
1、springboot2.2.2 ;
2、fastjson 1.1.26
3、<jackson-mapper-asl.version>1.9.10</jackson-mapper-asl.version>
<jackson-core.version>2.10.3</jackson-core.version>
第三點(diǎn)以及相關(guān)的依賴可能不需要,加上也沒關(guān)系
pom文件中的依賴:
<dependency> ?? ??? ??? ?<groupId>com.alibaba</groupId> ?? ??? ??? ?<artifactId>fastjson</artifactId> ?? ??? ??? ?<version>${fastjson.version}</version> ?? ??? ?</dependency> <dependency> ?? ??? ??? ?<groupId>org.codehaus.jackson</groupId> ?? ??? ??? ?<artifactId>jackson-mapper-asl</artifactId> ?? ??? ??? ?<version>${jackson-mapper-asl.version}</version> ?? ??? ?</dependency>? ?? ??? ?<dependency> ?? ??? ??? ?<groupId>com.fasterxml.jackson.core</groupId> ?? ??? ??? ?<artifactId>jackson-core</artifactId> ?? ??? ??? ?<version>${jackson-core.version}</version> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>com.fasterxml.jackson.core</groupId> ?? ??? ??? ?<artifactId>jackson-databind</artifactId> ?? ??? ??? ?<version>${jackson-core.version}</version> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>com.fasterxml.jackson.core</groupId> ?? ??? ??? ?<artifactId>jackson-annotations</artifactId> ?? ??? ??? ?<version>${jackson-core.version}</version> ?? ??? ?</dependency>
解決方式
第一步新建一個(gè)轉(zhuǎn)換類 用于自定義Jackson反序列化日期類型時(shí)應(yīng)用的類型轉(zhuǎn)換器,一般用于@RequestBody接受參數(shù)時(shí)使用
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反序列化日期類型時(shí)應(yīng)用的類型轉(zhuǎn)換器,一般用于@RequestBody接受參數(shù)時(shí)使用 * 次類是借用網(wǎng)上別的人的,增加了一個(gè)日期格式化的類型 */ public class DateJacksonConverter extends JsonDeserializer<Date> { //此處尤為重要,請(qǐng)查找自己控制臺(tái)報(bào)錯(cuò)的日期格式化類型是啥樣的 //我的是2020-04-29T16:23:44.999Z 所以我在下面的數(shù)組中添加了 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 格式;如果下面數(shù)組中沒有你要的格式化類型,其他的可自行根據(jù)自己的情況去添加即可 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; } }
創(chuàng)建好上邊的類之后,如果不想繼續(xù),則可以在實(shí)體對(duì)象屬性上加注解就完全可以了。
導(dǎo)入的包路徑為:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;?
@JsonDeserialize(using=DateJacksonConverter.class) private Date startDate;
如果不想去讓實(shí)體類加這個(gè)注解,可以在寫一個(gè)公共配置,如下:
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; } }
此時(shí)就不必再每個(gè)實(shí)體類屬性上加注解了。
@JsonFormat和@DateTimeFormat的作用
@DatetimeFormat
是將String轉(zhuǎn)換成Date,一般前臺(tái)給后臺(tái)傳值時(shí)用
? ? import org.springframework.format.annotation.DateTimeFormat; ? ? /** ? ? ?* 前臺(tái)傳后臺(tái)時(shí), 字符串自動(dòng)封裝成日期 ? ? ?*/ ? ? @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? private Date birth;
@JsonFormat(pattern=”yyyy-MM-dd”)
將Date轉(zhuǎn)換成String 一般后臺(tái)傳值給前臺(tái)時(shí)
? ? import com.fasterxml.jackson.annotation.JsonFormat; ? ? /** ? ? ?* 后臺(tái)返給前臺(tái)時(shí), 日期自動(dòng)格式化 ? ? ?*/ ? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? private Date birth;
注意:
@JsonFormat不僅可以完成后臺(tái)到前臺(tái)參數(shù)傳遞的類型轉(zhuǎn)換,還可以實(shí)現(xiàn)前臺(tái)到后臺(tái)類型轉(zhuǎn)換。
當(dāng)content-type為application/json時(shí),優(yōu)先使用@JsonFormat的pattern進(jìn)行類型轉(zhuǎn)換。而不會(huì)使用@DateTimeFormat進(jìn)行類型轉(zhuǎn)換。
@JsonFormat注解的作用就是完成json字符串到j(luò)ava對(duì)象的轉(zhuǎn)換工作,與參數(shù)傳遞的方向無(wú)關(guān)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式總結(jié)
Spring Task 是 Spring 框架提供的一種任務(wù)調(diào)度和異步處理的解決方案,可以按照約定的時(shí)間自動(dòng)執(zhí)行某個(gè)代碼邏輯它可以幫助開發(fā)者在 Spring 應(yīng)用中輕松地實(shí)現(xiàn)定時(shí)任務(wù)、異步任務(wù)等功能,提高應(yīng)用的效率和可維護(hù)性,需要的朋友可以參考下本文2024-07-07SpringBoot淺析安全管理之基于數(shù)據(jù)庫(kù)認(rèn)證
在真實(shí)的項(xiàng)目中,用戶的基本信息以及角色等都存儲(chǔ)在數(shù)據(jù)庫(kù)中,因此需要從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)進(jìn)行認(rèn)證和授權(quán)2022-08-08MyBatis超詳細(xì)講解如何實(shí)現(xiàn)分頁(yè)功能
MyBatis-Plus?是一個(gè)?Mybatis?增強(qiáng)版工具,在?MyBatis?上擴(kuò)充了其他功能沒有改變其基本功能,為了簡(jiǎn)化開發(fā)提交效率而存在,本篇文章帶用它實(shí)現(xiàn)分頁(yè)功能2022-03-03Java模擬登錄正方教務(wù)抓取成績(jī)、課表、空教室
這篇文章主要介紹了Java模擬登錄正方教務(wù)抓取成績(jī)、課表、空教室等信息,Java實(shí)現(xiàn)模擬登錄正方教務(wù)抓取成績(jī)、課表、空教室,通過(guò)HttpClient來(lái)模擬瀏覽器請(qǐng)求,Jsoup解析網(wǎng)頁(yè)內(nèi)容,感興趣的小伙伴們可以參考一下2016-04-04Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程
這是一個(gè)基于java的畢業(yè)設(shè)計(jì)項(xiàng)目,畢設(shè)課題為springboot框架的知識(shí)產(chǎn)權(quán)服務(wù)平臺(tái)系統(tǒng),是一個(gè)采用b/s結(jié)構(gòu)的javaweb項(xiàng)目,需要的朋友可以參考下2022-06-06