SpringBoot利用jackson格式化時間的三種方法
在實際開發中我們經常會與時間打交道,那這就會涉及到一個時間格式轉換的問題。接下來會介紹幾種在SpirngBoot中如何對時間格式進行轉換。
準備工作創建項目,添加依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
創建實體類UserDTO
添加屬性,get、set方法省略。
private String id;private String username;private Date createTime;
創建UserController
編寫控制層代碼
@RestControllerpublic class UserController { @GetMapping('/getUser') public List<UserDTO> getUser() {List<UserDTO> userList = new ArrayList<UserDTO>();for (int i=1; i<=3; i++) { UserDTO user = new UserDTO(); user.setCreateTime(new Date()); user.setUsername('gongj' + i); user.setId('j' + i); userList.add(user);}return userList; } }
調用接口:http://localhost:8080/getUser
該結果很顯然不是我們所需要的,所以我們需要進行時間格式化一下。而且還有時區問題,我當前時間是晚上 22:44。
第一種 使用注解在需要轉換的字段上增加 @JsonFormat注解,該注解是 jackson的,web 包集成了。
import com.fasterxml.jackson.annotation.JsonFormat;private String id;private String username; @JsonFormat(pattern = 'yyyy-MM-dd HH:mm:ss',timezone = 'GMT+8')private Date createTime;
pattern:需要轉換的時間日期的格式
timezone:時間設置為東八區,避免時間在轉換中有誤差
調用接口:http://localhost:8080/getUser
完成,但是這種也有不好的地方,如果我有一百個實體中都有 Date類型,那就要在一百個實體加入注解。顯得有點麻煩。
第二種 修改默認配置所有的json生成都離不開相關的HttpMessageConverters
SpringBoot 默認使用 jackson,并對其默認做了配置。所以我們來修改一下。
全局搜索 JacksonHttpMessageConvertersConfiguration。idea快捷鍵:Ctrl + shift + r
該類中有個方法mappingJackson2HttpMessageConverter 就是用來處理json的。
@Bean@ConditionalOnMissingBean(value = {MappingJackson2HttpMessageConverter.class},ignoredType = {'org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter', 'org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter'})MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {return new MappingJackson2HttpMessageConverter(objectMapper);}
注意該方法上有兩個注解,@Bean 注解就不在介紹了。介紹一下 ConditionalOnMissingBean注解。
@ConditionalOnMissingBean :當給定的在bean不存在時,則實例化當前 Bean。
打個比喻:你入職報到,你公司看你帶了電腦,就讓你使用你自己的電腦,如果你沒帶電腦,就讓你使用公司的電腦。SpringBoot 也是這樣子做的,你不提供,就使用默認的。
新建MyConfig
import java.text.SimpleDateFormat;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.fasterxml.jackson.databind.ObjectMapper;@Configurationpublic class MyConfig { @Bean MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverterConfiguration() {MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();ObjectMapper om = new ObjectMapper();//全局修改josn時間格式om.setDateFormat(new SimpleDateFormat('yyyy/MM/dd HH:mm:ss'));converter.setObjectMapper(om);return converter; }}
提供了一個 MappingJackson2HttpMessageConverter的 Bean ,所以Springboot就會使用我們所提供的。
將User實體的注解注釋
調用接口:http://localhost:8080/getUser
OK,這種方式也是可以的。
提供ObjectMapper
也可以提供一個 ObjectMapper,將上述提供的 MappingJackson2HttpMessageConverter進行注釋掉。
import java.text.SimpleDateFormat;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.fasterxml.jackson.databind.ObjectMapper;@BeanObjectMapper objectMapper() {ObjectMapper om = new ObjectMapper();om.setDateFormat(new SimpleDateFormat('yyyy-MM-dd'));return om;}
調用接口:http://localhost:8080/getUser
注意:上述兩種方法都是全局修改的哦!
第三種 配置文件修改在 application.yml或者properties中修改默認配置
yml
spring: jackson: date-format: yyyy/MM/dd timezone: GMT+8
properties
spring.jackson.date-format=yyyy-MM-dd HH:mmspring.jackson.time-zone=GMT+8
如果第二種方式和第三種方式配置同時存在,以第二種方式為主。
如果三種方式都存在的時候,以實體類中注解格式為主。
總結到此這篇關于SpringBoot利用jackson格式化時間的文章就介紹到這了,更多相關SpringBoot jackson格式化時間內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
