springboot2中使用@JsonFormat注解不生效的解決
使用@JsonFormat注解不生效
百度了不少解決方式,有讓用@JsonField,也有讓把fastjson去掉的,也有加很多配置的,但是都沒用。
本次使用的版本號
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>解決方式
第一步新建一個轉(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)上別的人的,增加了一個日期格式化的類型
*/
public class DateJacksonConverter extends JsonDeserializer<Date> {
//此處尤為重要,請查找自己控制臺報(bào)錯的日期格式化類型是啥樣的
//我的是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í)體對象屬性上加注解就完全可以了。
導(dǎo)入的包路徑為:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;?
@JsonDeserialize(using=DateJacksonConverter.class) private Date startDate;
如果不想去讓實(shí)體類加這個注解,可以在寫一個公共配置,如下:
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í)就不必再每個實(shí)體類屬性上加注解了。
@JsonFormat和@DateTimeFormat的作用
@DatetimeFormat是將String轉(zhuǎn)換成Date,一般前臺給后臺傳值時(shí)用
? ? import org.springframework.format.annotation.DateTimeFormat; ? ? /** ? ? ?* 前臺傳后臺時(shí), 字符串自動封裝成日期 ? ? ?*/ ? ? @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? private Date birth;
@JsonFormat(pattern=”yyyy-MM-dd”)將Date轉(zhuǎn)換成String 一般后臺傳值給前臺時(shí)
? ? import com.fasterxml.jackson.annotation.JsonFormat; ? ? /** ? ? ?* 后臺返給前臺時(shí), 日期自動格式化 ? ? ?*/ ? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? private Date birth;
注意:
@JsonFormat不僅可以完成后臺到前臺參數(shù)傳遞的類型轉(zhuǎn)換,還可以實(shí)現(xiàn)前臺到后臺類型轉(zhuǎn)換。
當(dāng)content-type為application/json時(shí),優(yōu)先使用@JsonFormat的pattern進(jìn)行類型轉(zhuǎn)換。而不會使用@DateTimeFormat進(jìn)行類型轉(zhuǎn)換。
@JsonFormat注解的作用就是完成json字符串到j(luò)ava對象的轉(zhuǎn)換工作,與參數(shù)傳遞的方向無關(guān)。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot @ConditionalOnMissingBean注解的作用詳解
- SpringBoot 注解事務(wù)聲明式事務(wù)的方式
- SpringBoot中@Pattern注解對時(shí)間格式校驗(yàn)方式
- springboot FeignClient注解及參數(shù)
- SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
- SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
- 親測SpringBoot參數(shù)傳遞及@RequestBody注解---踩過的坑及解決
- 詳解SpringBoot 快速整合Mybatis(去XML化+注解進(jìn)階)
- SpringBoot單元測試中@SpyBean使用小結(jié)
相關(guān)文章
java?Date和SimpleDateFormat時(shí)間類詳解
這篇文章主要介紹了java?Date和SimpleDateFormat時(shí)間類詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
MyBatis中的SQL映射文件如何配置參數(shù)映射和使用方法
MyBatis 是一種開源的 Java 持久化框架,它可以自動將數(shù)據(jù)庫中的數(shù)據(jù)映射到 Java 對象中,并且使得 Java 對象可以非常方便地存儲到數(shù)據(jù)庫中,本文將介紹 MyBatis 中 SQL 映射文件的參數(shù)映射配置和使用方法,需要的朋友可以參考下2023-07-07
IDEA如何設(shè)置SVN提交忽略文件 target.iml
使用IDEA的SVN插件時(shí),可能會遇到提交不必要文件的問題,解決這個問題有兩種方法:第一種是在IDEA設(shè)置中的File Types下的Ignore files and folders添加需要忽略的文件或文件夾;第二種是使用SVN客戶端TortoiseSVN,在項(xiàng)目目錄點(diǎn)擊右鍵選擇properties2024-10-10
Java 數(shù)據(jù)流之Broadcast State
這篇文章主要介紹了Java 數(shù)據(jù)流之Broadcast State,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐
這篇文章主要介紹了Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
springboot 自定義配置Boolean屬性不生效的解決
這篇文章主要介紹了springboot 自定義配置Boolean屬性不生效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
mybatis報(bào)錯元素內(nèi)容必須由格式正確的字符數(shù)據(jù)或標(biāo)記組成異常的解決辦法
今天小編就為大家分享一篇關(guān)于mybatis查詢出錯解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

