1.mapperObject对象
@Configuration
public class MapperObjectConfig {
@Bean(name = "mapperObject")
public ObjectMapper getObjectMapper() {
return new ObjectMapper().registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
}
}另外一种方法,在yml文件中配置这个就不需要mapperObject对象了
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss2.实体类的LocalDate/LocalDateTime属性上加注解
public class TimeBean {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime nowTime;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate nowDate;
}3.直接Controller接收实体对象
@RequestMapping("/test")
public void restController(TimeBean bean) {
System.out.println(bean.getNowDate());
System.out.println(bean.getNowTime());
}前端参数示例:
注意 LocalDateTime不能使用 pattern ="yyyy-MM-dd"
否则报错:
Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'nowTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2017-07-02 11:10:11'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2017-07-02 11:10:11]没错,就是这么简单....
版权声明:本文为A1962747112原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。