Spring boot SpringBoot TestRestTemplate和LocalDateTime不工作
在包含LocalDateTimeDeserializer之后,我在模型中使用LocalDateTime, 将bean字段转换为Spring boot SpringBoot TestRestTemplate和LocalDateTime不工作,spring-boot,resttemplate,spring-test,spring-rest,localdate,Spring Boot,Resttemplate,Spring Test,Spring Rest,Localdate,在包含LocalDateTimeDeserializer之后,我在模型中使用LocalDateTime, 将bean字段转换为 @NotNull @Column(name = "created") @JsonDeserialize(using = LocalDateTimeDeserializer.class) private LocalDateTime created; 包括 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = f
@NotNull
@Column(name = "created")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime created;
包括
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
属性,应用程序最终能够反序列化JSON并正确显示如下内容:
"created": "2018-04-22T21:21:53.025",
但是,当我进行测试时,它会忽略WRITE_DATES_AS_TIMESTAMPS标志,我猜它会为上面相同的字符串日期生成一个输出
"created":{"year":2018,"monthValue":4,"month":"APRIL","dayOfMonth":22,"dayOfYear":112,"dayOfWeek":"SUNDAY","hour":21,"minute":23,"second":16,"nano":986000000,"chronology":{"id":"ISO","calendarType":"iso8601"}}
请注意,在测试资源文件夹的application.properties中包含该属性不会改变任何内容
我的测试配置看起来像
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Category(IntegrationTest.class)
public class ApplicationTests {
....
public class LocalDateTimeCustomSerializer extends LocalDateTimeSerializer {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
LocalDateTimeCustomSerializer() {
this(FORMATTER);
}
public LocalDateTimeCustomSerializer(DateTimeFormatter f) {
super(f);
}
@Override
protected DateTimeFormatter _defaultFormatter() {
return FORMATTER;
}
}
你知道我做错了什么吗?我也有同样的问题,下面的解决方案对我有效 在Applicationtests类中添加以下代码
protected MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
@Autowired
public void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter)Arrays.asList(converters).stream()
.filter(hmc -> hmc instanceof MappingJackson2HttpMessageConverter)
.findAny()
.orElse(null);
assertNotNull("the JSON message converter must not be null",
this.mappingJackson2HttpMessageConverter);
final ObjectMapper objectMapper = new ObjectMapper();
final JavaTimeModule javaTimeModule = new JavaTimeModule();
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
}
其中自定义类看起来像
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Category(IntegrationTest.class)
public class ApplicationTests {
....
public class LocalDateTimeCustomSerializer extends LocalDateTimeSerializer {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
LocalDateTimeCustomSerializer() {
this(FORMATTER);
}
public LocalDateTimeCustomSerializer(DateTimeFormatter f) {
super(f);
}
@Override
protected DateTimeFormatter _defaultFormatter() {
return FORMATTER;
}
}
及
我有同样的问题,下面的解决方案对我有效 在Applicationtests类中添加以下代码
protected MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
@Autowired
public void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter)Arrays.asList(converters).stream()
.filter(hmc -> hmc instanceof MappingJackson2HttpMessageConverter)
.findAny()
.orElse(null);
assertNotNull("the JSON message converter must not be null",
this.mappingJackson2HttpMessageConverter);
final ObjectMapper objectMapper = new ObjectMapper();
final JavaTimeModule javaTimeModule = new JavaTimeModule();
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
}
其中自定义类看起来像
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Category(IntegrationTest.class)
public class ApplicationTests {
....
public class LocalDateTimeCustomSerializer extends LocalDateTimeSerializer {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
LocalDateTimeCustomSerializer() {
this(FORMATTER);
}
public LocalDateTimeCustomSerializer(DateTimeFormatter f) {
super(f);
}
@Override
protected DateTimeFormatter _defaultFormatter() {
return FORMATTER;
}
}
及