application.yml:
user:
id: 1
name: jarvis
birth: 1996-05-01 23:25:04
User类:
@Data
@Component
@ConfigurationProperties("user")
public class User {
private Integer id;
private String name;
@JsonFormat(timezone = "GMT+8",pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime birth;
}
@JsonFormat(timezone = “GMT+8”,pattern=“yyyy-MM-dd HH:mm:ss”)
解决后台到前台时间格式的转换;只在返回json时起作用
-------pattern:需要转换的时间日期的格式
-------timezone:时间设置为东八区,避免时间在转换中有误差
@DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”)
解决前后到后台的时间格式的转换
-------pattern:需要转换的时间日期的格式
测试:
@RestController
@RequestMapping("/user")
@Slf4j(topic = "UserController")
public class UserController {
@Autowired
ApplicationContext context;
@GetMapping("/getUserInfo")
public String getUserInfo(){
log.info(context.getBean(User.class).toString());
return "asdf";
}
}
输出:
[nio-8080-exec-1] UserController: User(id=1, name=test, birth=1996-05-01T23:25:04)