-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Description
There are some issues with the ObjectMapper generated by the default configuration in the Spring context. When using the readerWithView() method to deserialize objects, it results in an error (all properties of the generated object are null). This directly affects Spring Messaging because MappingJackson2MessageConverter uses this method for deserialization, causing failures in Spring Messaging. However, when I use an ObjectMapper created with Jackson's default configuration, the issue does not occur. I created a Test project kotlin , java to verify this, and below is the simplified code for the test:
Different:
Spring ObjectMapper: MapperFeature.DEFAULT_VIEW_INCLUSION is false
Default ObjectMapper: MapperFeature.DEFAULT_VIEW_INCLUSION is true
I don't know this a wrong configuration or Jackson's bug!
Spring boot version is 3.3.4
@ExtendWith({SpringExtension.class})
@SpringBootTest(classes = Main.class)
public class JsonTest {
static class SimpleStockFreezeStatusMessage {
private Long businessId;
private Boolean success;
private String tenantId;
// getter and setter ...
}
public void testSpringJsonMessage(ObjectMapper objectMapper) throws IOException {
var json = "{\"businessId\":1336504106360835,\"tenantId\":\"first\",\"success\":false}";
var value = objectMapper.readValue(json, SimpleStockFreezeStatusMessage.class);
System.out.println("message ser by readValue : " + value.tenantId);
// success
assertEquals(value.tenantId, "first");
Class<?> view = SimpleStockFreezeStatusMessage.class;
JavaType javaType = objectMapper.constructType(new TypeReference<SimpleStockFreezeStatusMessage>() {});
//this is Spring message Usage
var value2 = (SimpleStockFreezeStatusMessage)objectMapper.readerWithView(view).forType(javaType).readValue(json);
System.out.println("message ser by readerWithView: " + value2.tenantId);
// tenantId is null
assertEquals(value2.tenantId, "first");
}
@Autowired
private ObjectMapper objectMapper;
//Fails
@Test
public void testBySpringMapper() throws IOException {
testSpringJsonMessage(objectMapper);
}
//Success
@Test
public void testByDefaultMapper() throws IOException {
testSpringJsonMessage(new ObjectMapper());
}
}
The same issue also exists in version 3.2.9