Add constructors to take ObjectMapper

Prior to this commit in the message converters it was possible
to set a pre-configured ObjectMapper. However the constructor
would still create and configure an ObjectMapper.

With the added constructor it is now possible to directly
construct the message converter with the proper ObjectMapper.
This prevents the this additional ObjectMapper to be constructed.
This commit is contained in:
Marten Deinum 2023-09-14 15:39:57 +02:00 committed by Juergen Hoeller
parent f628c601a7
commit e42e89c04f
2 changed files with 12 additions and 0 deletions

View File

@ -96,6 +96,11 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public MappingJackson2MessageConverter(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
}
/**
* Specify the {@link ObjectMapper} to use instead of using the default.
*/

View File

@ -85,6 +85,13 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
this.objectMapper = initObjectMapper();
}
public MappingJackson2MessageConverter(ObjectMapper objectMapper) {
super(new MimeType("application", "json"), new MimeType("application", "*+json"));
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
}
@SuppressWarnings("deprecation") // on Jackson 2.13: configure(MapperFeature, boolean)
private ObjectMapper initObjectMapper() {