Add builder-based constructors to Jackson 3 infrastructure

This commit also polishes documentation, constructor parameter and
field names.

Closes gh-35597
This commit is contained in:
Sébastien Deleuze
2025-10-15 11:49:55 +02:00
parent 3493fc9324
commit 8ec9ab6698
17 changed files with 325 additions and 119 deletions
@@ -51,17 +51,38 @@ public class JacksonCborDecoder extends AbstractJacksonDecoder<CBORMapper> {
super(CBORMapper.builder(), MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* @see CBORMapper#builder()
*/
public JacksonCborDecoder(CBORMapper.Builder builder) {
super(builder, MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper}.
* @see CBORMapper#builder()
*/
public JacksonCborDecoder(CBORMapper mapper) {
super(mapper, MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}, and
* {@link MimeType}s.
* @see CBORMapper#builder()
*/
public JacksonCborDecoder(CBORMapper.Builder builder, MimeType... mimeTypes) {
super(builder, mimeTypes);
}
/**
* Construct a new instance with the provided {@link CBORMapper} and {@link MimeType}s.
* @see CBORMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonCborDecoder(CBORMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
@@ -52,19 +52,38 @@ public class JacksonCborEncoder extends AbstractJacksonEncoder<CBORMapper> {
super(CBORMapper.builder(), MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* @see CBORMapper#builder()
*/
public JacksonCborEncoder(CBORMapper.Builder builder) {
super(builder, MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper}.
* @see CBORMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonCborEncoder(CBORMapper mapper) {
super(mapper, MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}, and
* {@link MimeType}s.
* @see CBORMapper#builder()
*/
public JacksonCborEncoder(CBORMapper.Builder builder, MimeType... mimeTypes) {
super(builder, mimeTypes);
}
/**
* Construct a new instance with the provided {@link CBORMapper} and {@link MimeType}s.
* @see CBORMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonCborEncoder(CBORMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
@@ -42,9 +42,6 @@ import org.springframework.util.MimeTypeUtils;
* <a href="https://github.com/FasterXML/jackson">Jackson 3.x</a>
* leveraging non-blocking parsing.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* @author Sebastien Deleuze
* @since 7.0
* @see JacksonJsonEncoder
@@ -71,19 +68,37 @@ public class JacksonJsonDecoder extends AbstractJacksonDecoder<JsonMapper> {
super(JsonMapper.builder(), DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link JsonMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* @see JsonMapper#builder()
*/
public JacksonJsonDecoder(JsonMapper.Builder builder) {
super(builder, DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonJsonDecoder(JsonMapper mapper) {
this(mapper, DEFAULT_JSON_MIME_TYPES);
super(mapper, DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link JsonMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}, and
* {@link MimeType}s.
* @see JsonMapper#builder()
*/
public JacksonJsonDecoder(JsonMapper.Builder builder, MimeType... mimeTypes) {
super(builder, mimeTypes);
}
/**
* Construct a new instance with the provided {@link JsonMapper} and {@link MimeType}s.
* @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonJsonDecoder(JsonMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
@@ -43,9 +43,6 @@ import org.springframework.util.MimeType;
* use cases, {@link Flux} elements are collected into a {@link List} before
* serialization for performance reason.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* @author Sebastien Deleuze
* @since 7.0
* @see JacksonJsonDecoder
@@ -72,26 +69,44 @@ public class JacksonJsonEncoder extends AbstractJacksonEncoder<JsonMapper> {
* {@link ProblemDetailJacksonMixin}.
*/
public JacksonJsonEncoder() {
super(JsonMapper.builder().addMixIn(ProblemDetail.class, ProblemDetailJacksonMixin.class),
DEFAULT_JSON_MIME_TYPES);
setStreamingMediaTypes(List.of(MediaType.APPLICATION_NDJSON));
this.ssePrettyPrinter = initSsePrettyPrinter();
this(JsonMapper.builder(), DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with a {@link JsonMapper.Builder} customized
* with the {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)} and
* {@link ProblemDetailJacksonMixin}.
* @see JsonMapper#builder()
*/
public JacksonJsonEncoder(JsonMapper.Builder builder) {
this(builder, DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonJsonEncoder(JsonMapper mapper) {
this(mapper, DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link JsonMapper.Builder} customized
* with the {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)} and
* {@link ProblemDetailJacksonMixin}, and {@link MimeType}s.
* @see JsonMapper#builder()
*/
public JacksonJsonEncoder(JsonMapper.Builder builder, MimeType... mimeTypes) {
super(builder.addMixIn(ProblemDetail.class, ProblemDetailJacksonMixin.class), mimeTypes);
setStreamingMediaTypes(List.of(MediaType.APPLICATION_NDJSON));
this.ssePrettyPrinter = initSsePrettyPrinter();
}
/**
* Construct a new instance with the provided {@link JsonMapper} and
* {@link MimeType}s.
* @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonJsonEncoder(JsonMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
@@ -26,9 +26,6 @@ import org.springframework.util.MimeType;
* Decode a byte stream into Smile and convert to Objects with Jackson 3.x,
* leveraging non-blocking parsing.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* @author Sebastien Deleuze
* @since 7.0
* @see JacksonSmileEncoder
@@ -44,24 +41,44 @@ public class JacksonSmileDecoder extends AbstractJacksonDecoder<SmileMapper> {
* Construct a new instance with a {@link SmileMapper} customized with the
* {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)}.
* @see SmileMapper#builder()
*/
public JacksonSmileDecoder() {
super(SmileMapper.builder(), DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* @see SmileMapper#builder()
*/
public JacksonSmileDecoder(SmileMapper.Builder builder) {
this(builder, DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper}.
* @see SmileMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonSmileDecoder(SmileMapper mapper) {
this(mapper, DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}, and
* {@link MimeType}s.
* @see SmileMapper#builder()
*/
public JacksonSmileDecoder(SmileMapper.Builder builder, MimeType... mimeTypes) {
super(builder, mimeTypes);
}
/**
* Construct a new instance with the provided {@link SmileMapper} and {@link MimeType}s.
* @see SmileMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonSmileDecoder(SmileMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
@@ -34,9 +34,6 @@ import org.springframework.util.MimeType;
* <p>For non-streaming use cases, {@link Flux} elements are collected into a {@link List}
* before serialization for performance reasons.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* @author Sebastien Deleuze
* @since 7.0
* @see JacksonSmileDecoder
@@ -59,24 +56,42 @@ public class JacksonSmileEncoder extends AbstractJacksonEncoder<SmileMapper> {
* {@link MapperBuilder#findModules(ClassLoader)}.
*/
public JacksonSmileEncoder() {
super(SmileMapper.builder(), DEFAULT_SMILE_MIME_TYPES);
setStreamingMediaTypes(Collections.singletonList(DEFAULT_SMILE_STREAMING_MEDIA_TYPE));
this(SmileMapper.builder(), DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* @see SmileMapper#builder()
*/
public JacksonSmileEncoder(SmileMapper.Builder builder) {
this(builder, DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper}.
* @see SmileMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonSmileEncoder(SmileMapper mapper) {
super(mapper, DEFAULT_SMILE_MIME_TYPES);
this(mapper, DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}, and
* {@link MimeType}s.
* @see SmileMapper#builder()
*/
public JacksonSmileEncoder(SmileMapper.Builder builder, MimeType... mimeTypes) {
super(builder, mimeTypes);
setStreamingMediaTypes(Collections.singletonList(DEFAULT_SMILE_STREAMING_MEDIA_TYPE));
}
/**
* Construct a new instance with the provided {@link SmileMapper} and {@link MimeType}s.
* @see SmileMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonSmileEncoder(SmileMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
@@ -32,8 +32,13 @@ import org.springframework.http.converter.AbstractJacksonHttpMessageConverter;
* media type. This can be overridden by setting the {@link #setSupportedMediaTypes
* supportedMediaTypes} property.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* <p>The following hints entries are supported:
* <ul>
* <li>A JSON view with a <code>"com.fasterxml.jackson.annotation.JsonView"</code>
* key and the class name of the JSON view as value.</li>
* <li>A filter provider with a <code>"tools.jackson.databind.ser.FilterProvider"</code>
* key and the filter provider class name as value.</li>
* </ul>
*
* @author Sebastien Deleuze
* @since 7.0
@@ -49,10 +54,19 @@ public class JacksonCborHttpMessageConverter extends AbstractJacksonHttpMessageC
super(CBORMapper.builder(), MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* @see CBORMapper#builder()
*/
public JacksonCborHttpMessageConverter(CBORMapper.Builder builder) {
super(builder, MediaType.APPLICATION_CBOR);
}
/**
* Construct a new instance with the provided {@link CBORMapper}.
* @see CBORMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonCborHttpMessageConverter(CBORMapper mapper) {
super(mapper, MediaType.APPLICATION_CBOR);
@@ -41,14 +41,11 @@ import org.springframework.http.converter.AbstractJacksonHttpMessageConverter;
* can be overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes}
* property.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* <p>The following hints entries are supported:
* <ul>
* <li>A JSON view with a <code>com.fasterxml.jackson.annotation.JsonView</code>
* <li>A JSON view with a <code>"com.fasterxml.jackson.annotation.JsonView"</code>
* key and the class name of the JSON view as value.</li>
* <li>A filter provider with a <code>tools.jackson.databind.ser.FilterProvider</code>
* <li>A filter provider with a <code>"tools.jackson.databind.ser.FilterProvider"</code>
* key and the filter provider class name as value.</li>
* </ul>
*
@@ -74,16 +71,26 @@ public class JacksonJsonHttpMessageConverter extends AbstractJacksonHttpMessageC
* {@link ProblemDetailJacksonMixin}.
*/
public JacksonJsonHttpMessageConverter() {
super(JsonMapper.builder().addMixIn(ProblemDetail.class, ProblemDetailJacksonMixin.class), DEFAULT_JSON_MIME_TYPES);
this(JsonMapper.builder());
}
/**
* Construct a new instance with the provided {@link JsonMapper.Builder}
* customized with the {@link tools.jackson.databind.JacksonModule}s found
* by {@link MapperBuilder#findModules(ClassLoader)} and
* {@link ProblemDetailJacksonMixin}.
* @see JsonMapper#builder()
*/
public JacksonJsonHttpMessageConverter(JsonMapper.Builder builder) {
super(builder.addMixIn(ProblemDetail.class, ProblemDetailJacksonMixin.class), DEFAULT_JSON_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonJsonHttpMessageConverter(JsonMapper objectMapper) {
super(objectMapper, DEFAULT_JSON_MIME_TYPES);
public JacksonJsonHttpMessageConverter(JsonMapper mapper) {
super(mapper, DEFAULT_JSON_MIME_TYPES);
}
@@ -32,8 +32,13 @@ import org.springframework.http.converter.AbstractJacksonHttpMessageConverter;
* media type. This can be overridden by setting the
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* <p>The following hints entries are supported:
* <ul>
* <li>A JSON view with a <code>"com.fasterxml.jackson.annotation.JsonView"</code>
* key and the class name of the JSON view as value.</li>
* <li>A filter provider with a <code>"tools.jackson.databind.ser.FilterProvider"</code>
* key and the filter provider class name as value.</li>
* </ul>
*
* @author Sebastien Deleuze
* @since 7.0
@@ -51,10 +56,19 @@ public class JacksonSmileHttpMessageConverter extends AbstractJacksonHttpMessage
super(SmileMapper.builder(), DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper} customized
* with the {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)}.
* @see SmileMapper#builder()
*/
public JacksonSmileHttpMessageConverter(SmileMapper.Builder builder) {
super(builder, DEFAULT_SMILE_MIME_TYPES);
}
/**
* Construct a new instance with the provided {@link SmileMapper}.
* @see SmileMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonSmileHttpMessageConverter(SmileMapper mapper) {
super(mapper, DEFAULT_SMILE_MIME_TYPES);
@@ -39,9 +39,6 @@ import org.springframework.util.xml.StaxUtils;
* {@code application/*+xml} with {@code UTF-8} character set. This can be overridden by
* setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* <p>The following hint entries are supported:
* <ul>
* <li>A JSON view with a <code>com.fasterxml.jackson.annotation.JsonView</code>
@@ -80,6 +77,7 @@ public class JacksonXmlHttpMessageConverter extends AbstractJacksonHttpMessageCo
* customized with the {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)} and
* {@link ProblemDetailJacksonXmlMixin}.
* @see XmlMapper#builder()
*/
public JacksonXmlHttpMessageConverter(XmlMapper.Builder builder) {
super(builder.addMixIn(ProblemDetail.class, ProblemDetailJacksonXmlMixin.class), DEFAULT_XML_MIME_TYPES);
@@ -88,7 +86,6 @@ public class JacksonXmlHttpMessageConverter extends AbstractJacksonHttpMessageCo
/**
* Construct a new instance with the provided {@link XmlMapper}.
* @see XmlMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonXmlHttpMessageConverter(XmlMapper xmlMapper) {
super(xmlMapper, DEFAULT_XML_MIME_TYPES);
@@ -32,8 +32,13 @@ import org.springframework.http.converter.AbstractJacksonHttpMessageConverter;
* media type. This can be overridden by setting the {@link #setSupportedMediaTypes
* supportedMediaTypes} property.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
* <p>The following hints entries are supported:
* <ul>
* <li>A JSON view with a <code>"com.fasterxml.jackson.annotation.JsonView"</code>
* key and the class name of the JSON view as value.</li>
* <li>A filter provider with a <code>"tools.jackson.databind.ser.FilterProvider"</code>
* key and the filter provider class name as value.</li>
* </ul>
*
* @author Sebastien Deleuze
* @since 7.0
@@ -52,7 +57,16 @@ public class JacksonYamlHttpMessageConverter extends AbstractJacksonHttpMessageC
/**
* Construct a new instance with the provided {@link YAMLMapper}.
* @see YAMLMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonYamlHttpMessageConverter(YAMLMapper.Builder builder) {
super(builder, MediaType.APPLICATION_YAML);
}
/**
* Construct a new instance with the provided {@link YAMLMapper.Builder} customized
* with the {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)}.
* @see YAMLMapper#builder()
*/
public JacksonYamlHttpMessageConverter(YAMLMapper mapper) {
super(mapper, MediaType.APPLICATION_YAML);