无法反序列化-Jackson LocalDate/Time-JUnit [英] Unable to Deserialize - Jackson LocalDate/Time - JUnit
问题描述
我需要在Junit中将Json反序列化为Java对象.我有
I need to deserialize the Json to Java Objects in Junit. I have Json file like
{
"studentId":57,
"JoinedDate":"31-12-2019",
"DOB":"08-06-1998"
}
我有相同的课程要映射
public class Student{
private long studentId ;
private LocalDate JoinedDate;
private LocalDate DOB ;
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public LocalDate getJoinedDate() {
return JoinedDate;
}
public void setJoinedDate(LocalDate joinedDate) {
JoinedDate = joinedDate;
}
public LocalDate getDOB() {
return DOB;
}
public void setDOB(LocalDate dOB) {
DOB = dOB;
}
我需要为类似这样的单元测试项目编写集中式生成器
I need to write centralized builder for Unit testing project similar like this
builder.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
主类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@WebAppConfiguration
public class Main{
@Test
public void contextLoads() {
assertTrue(true);
}
}
单元测试项目看起来像
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@WebAppConfiguration
public class StudentTest{
private ObjectMapper jsonObjectMapper;
@Before
public void setUp() throws IOException {
jsonObjectMapper = new ObjectMapper();
studentJson = IOUtils.toString(getClass().getResourceAsStream(CommonTestConstants.StudentPath+ "/Student.json"));
}
映射对象时出现错误-
com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串"31-12-2019"反序列化类型为java.time.LocalDate
的值:无法反序列化java.time.LocalDate:
I'm getting a error while mapping the objects -
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.LocalDate
from String "31-12-2019": Failed to deserialize java.time.LocalDate:
另一个错误-有时.
com.fasterxml.jackson.databind.JsonMappingException:文本'31 -12-2019' 无法在索引0处解析
com.fasterxml.jackson.databind.JsonMappingException: Text '31-12-2019' could not be parsed at index 0
我认为是LocalDate格式不匹配的问题.任何建议使其集中化而不是在字段上方指定格式的建议.有人请指教吗?
I assume LocalDate format mismatch is the issue. Any suggestion to make it centralized way instead of specifying the format above the fields. Any one please advise?
参考- Spring Boot JacksonTester自定义序列化器未注册 >
推荐答案
Springboot 1.4.x
或更高版本具有此接口Jackson2ObjectMapperBuilderCustomizer
,可用于初始化objectMapper
.
Springboot 1.4.x
or above has this interface Jackson2ObjectMapperBuilderCustomizer
which allows you to initialize objectMapper
.
我们需要做的是重写customize
方法并注册deserializers
和
serializers
.
What we need to do, is override customize
method and register deserializers
and
serializers
.
@SpringBootApplication
public class TestApplication implements Jackson2ObjectMapperBuilderCustomizer {
@Override
public void customize(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
// pattern could be anything whatever is required
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/dd/MM");
LocalDateSerializer localDateDeserializer = new LocalDateSerializer(formatter);
jackson2ObjectMapperBuilder
.failOnEmptyBeans(false)
.deserializersByType(new HashMap<Class<?>, JsonDeserializer<?>>(){{
put(LocalTime.class, localTimeSerializer);
}});
}
}
我们也可以添加seriliazers
类似的方式.
We can also add seriliazers
similar way.
jackson2ObjectMapperBuilder
.failOnEmptyBeans(false)
.serializersByType(new HashMap<Class<?>, JsonSerializer<?>>(){{
put(LocalTime.class, localTimeSerializer);
}});
you can check more details here. Spring Jackson builder
这篇关于无法反序列化-Jackson LocalDate/Time-JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!