这篇文章给大家分享的是有关SpringBoot如何利用jackson格式化时间的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创建项目,添加依赖
<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
编写控制层代码
@RestController
public 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;
@Configuration
public 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;
@Bean
ObjectMapper 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:mm
spring.jackson.time-zone=GMT+8
如果第二种方式和第三种方式配置同时存在,以第二种方式为主。
如果三种方式都存在的时候,以实体类中注解格式为主。
感谢各位的阅读!关于“SpringBoot如何利用jackson格式化时间”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。