Update reference documentation to use Jackson 3
Closes gh-35886
This commit is contained in:
parent
4ee0a8ee33
commit
fb9f31d101
|
|
@ -237,8 +237,8 @@ Java::
|
|||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
RSocketStrategies strategies = RSocketStrategies.builder()
|
||||
.encoders(encoders -> encoders.add(new Jackson2CborEncoder()))
|
||||
.decoders(decoders -> decoders.add(new Jackson2CborDecoder()))
|
||||
.encoders(encoders -> encoders.add(new JacksonCborEncoder()))
|
||||
.decoders(decoders -> decoders.add(new JacksonCborDecoder()))
|
||||
.build();
|
||||
|
||||
RSocketRequester requester = RSocketRequester.builder()
|
||||
|
|
@ -251,8 +251,8 @@ Kotlin::
|
|||
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
val strategies = RSocketStrategies.builder()
|
||||
.encoders { it.add(Jackson2CborEncoder()) }
|
||||
.decoders { it.add(Jackson2CborDecoder()) }
|
||||
.encoders { it.add(JacksonCborEncoder()) }
|
||||
.decoders { it.add(JacksonCborDecoder()) }
|
||||
.build()
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
|
|
@ -681,8 +681,8 @@ Java::
|
|||
@Bean
|
||||
public RSocketStrategies rsocketStrategies() {
|
||||
return RSocketStrategies.builder()
|
||||
.encoders(encoders -> encoders.add(new Jackson2CborEncoder()))
|
||||
.decoders(decoders -> decoders.add(new Jackson2CborDecoder()))
|
||||
.encoders(encoders -> encoders.add(new JacksonCborEncoder()))
|
||||
.decoders(decoders -> decoders.add(new JacksonCborDecoder()))
|
||||
.routeMatcher(new PathPatternRouteMatcher())
|
||||
.build();
|
||||
}
|
||||
|
|
@ -703,8 +703,8 @@ Kotlin::
|
|||
|
||||
@Bean
|
||||
fun rsocketStrategies() = RSocketStrategies.builder()
|
||||
.encoders { it.add(Jackson2CborEncoder()) }
|
||||
.decoders { it.add(Jackson2CborDecoder()) }
|
||||
.encoders { it.add(JacksonCborEncoder()) }
|
||||
.decoders { it.add(JacksonCborDecoder()) }
|
||||
.routeMatcher(PathPatternRouteMatcher())
|
||||
.build()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -374,14 +374,14 @@ Java::
|
|||
+
|
||||
[source,java]
|
||||
----
|
||||
ServerResponse.ok().hint(Jackson2CodecSupport.JSON_VIEW_HINT, MyJacksonView.class).body(...);
|
||||
ServerResponse.ok().hint(JacksonCodecSupport.JSON_VIEW_HINT, MyJacksonView.class).body(...);
|
||||
----
|
||||
|
||||
Kotlin::
|
||||
+
|
||||
[source,kotlin]
|
||||
----
|
||||
ServerResponse.ok().hint(Jackson2CodecSupport.JSON_VIEW_HINT, MyJacksonView::class.java).body(...)
|
||||
ServerResponse.ok().hint(JacksonCodecSupport.JSON_VIEW_HINT, MyJacksonView::class.java).body(...)
|
||||
----
|
||||
======
|
||||
|
||||
|
|
|
|||
|
|
@ -482,7 +482,7 @@ purposes, it is useful to be able to alternate between rendering a model with an
|
|||
or as other formats (such as JSON or XML), depending on the content type requested by the client.
|
||||
To support doing so, Spring WebFlux provides the `HttpMessageWriterView`, which you can use to
|
||||
plug in any of the available xref:web/webflux/reactive-spring.adoc#webflux-codecs[Codecs] from
|
||||
`spring-web`, such as `Jackson2JsonEncoder`, `Jackson2SmileEncoder`, or `Jaxb2XmlEncoder`.
|
||||
`spring-web`, such as `JacksonJsonEncoder`, `JacksonSmileEncoder`, or `Jaxb2XmlEncoder`.
|
||||
|
||||
Unlike other view technologies, `HttpMessageWriterView` does not require a `ViewResolver` but is
|
||||
instead xref:web/webflux/config.adoc#webflux-config-view-resolvers[configured] as a default view.
|
||||
|
|
|
|||
|
|
@ -334,19 +334,8 @@ Kotlin::
|
|||
`ServerCodecConfigurer` provides a set of default readers and writers. You can use it to add
|
||||
more readers and writers, customize the default ones, or replace the default ones completely.
|
||||
|
||||
For Jackson JSON and XML, consider using
|
||||
{spring-framework-api}/http/converter/json/Jackson2ObjectMapperBuilder.html[`Jackson2ObjectMapperBuilder`],
|
||||
which customizes Jackson's default properties with the following ones:
|
||||
|
||||
* {jackson-docs}/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/DeserializationFeature.html#FAIL_ON_UNKNOWN_PROPERTIES[`DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES`] is disabled.
|
||||
* {jackson-docs}/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/MapperFeature.html#DEFAULT_VIEW_INCLUSION[`MapperFeature.DEFAULT_VIEW_INCLUSION`] is disabled.
|
||||
|
||||
It also automatically registers the following well-known modules if they are detected on the classpath:
|
||||
|
||||
* {jackson-github-org}/jackson-datatype-jsr310[`jackson-datatype-jsr310`]: Support for Java 8 Date and Time API types.
|
||||
* {jackson-github-org}/jackson-datatype-jdk8[`jackson-datatype-jdk8`]: Support for other Java 8 types, such as `Optional`.
|
||||
* {jackson-github-org}/jackson-module-kotlin[`jackson-module-kotlin`]: Support for Kotlin classes and data classes.
|
||||
|
||||
For Jackson, consider using a Jackson format-specific builder like `JsonMapper.Builder` to configure Jackson's default
|
||||
properties.
|
||||
|
||||
[[webflux-config-view-resolvers]]
|
||||
== View Resolvers
|
||||
|
|
@ -489,7 +478,7 @@ Java::
|
|||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
registry.freeMarker();
|
||||
|
||||
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
|
||||
JacksonJsonEncoder encoder = new JacksonJsonEncoder();
|
||||
registry.defaultViews(new HttpMessageWriterView(encoder));
|
||||
}
|
||||
|
||||
|
|
@ -508,7 +497,7 @@ Kotlin::
|
|||
override fun configureViewResolvers(registry: ViewResolverRegistry) {
|
||||
registry.freeMarker()
|
||||
|
||||
val encoder = Jackson2JsonEncoder()
|
||||
val encoder = JacksonJsonEncoder()
|
||||
registry.defaultViews(HttpMessageWriterView(encoder))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -459,22 +459,22 @@ xref:web/webflux/config.adoc#webflux-config-message-codecs[HTTP message codecs].
|
|||
JSON and binary JSON ({jackson-github-org}/smile-format-specification[Smile]) are
|
||||
both supported when the Jackson library is present.
|
||||
|
||||
The `Jackson2Decoder` works as follows:
|
||||
The `JacksonJsonDecoder` works as follows:
|
||||
|
||||
* Jackson's asynchronous, non-blocking parser is used to aggregate a stream of byte chunks
|
||||
into ``TokenBuffer``'s each representing a JSON object.
|
||||
* Each `TokenBuffer` is passed to Jackson's `ObjectMapper` to create a higher level object.
|
||||
* Each `TokenBuffer` is passed to Jackson's `JsonMapper` to create a higher level object.
|
||||
* When decoding to a single-value publisher (for example, `Mono`), there is one `TokenBuffer`.
|
||||
* When decoding to a multi-value publisher (for example, `Flux`), each `TokenBuffer` is passed to
|
||||
the `ObjectMapper` as soon as enough bytes are received for a fully formed object. The
|
||||
the `JsonMapper` as soon as enough bytes are received for a fully formed object. The
|
||||
input content can be a JSON array, or any
|
||||
https://en.wikipedia.org/wiki/JSON_streaming[line-delimited JSON] format such as NDJSON,
|
||||
JSON Lines, or JSON Text Sequences.
|
||||
|
||||
The `Jackson2Encoder` works as follows:
|
||||
The `JacksonJsonEncoder` works as follows:
|
||||
|
||||
* For a single value publisher (for example, `Mono`), simply serialize it through the
|
||||
`ObjectMapper`.
|
||||
`JsonMapper`.
|
||||
* For a multi-value publisher with `application/json`, by default collect the values with
|
||||
`Flux#collectToList()` and then serialize the resulting collection.
|
||||
* For a multi-value publisher with a streaming media type such as
|
||||
|
|
@ -482,12 +482,12 @@ The `Jackson2Encoder` works as follows:
|
|||
flush each value individually using a
|
||||
https://en.wikipedia.org/wiki/JSON_streaming[line-delimited JSON] format. Other
|
||||
streaming media types may be registered with the encoder.
|
||||
* For SSE the `Jackson2Encoder` is invoked per event and the output is flushed to ensure
|
||||
* For SSE the `JacksonJsonEncoder` is invoked per event and the output is flushed to ensure
|
||||
delivery without delay.
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
By default both `Jackson2Encoder` and `Jackson2Decoder` do not support elements of type
|
||||
By default both `JacksonJsonEncoder` and `JacksonJsonDecoder` do not support elements of type
|
||||
`String`. Instead the default assumption is that a string or a sequence of strings
|
||||
represent serialized JSON content, to be rendered by the `CharSequenceEncoder`. If what
|
||||
you need is to render a JSON array from `Flux<String>`, use `Flux#collectToList()` and
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ Spring offers support for the Jackson JSON library.
|
|||
== Jackson-based JSON MVC Views
|
||||
[.small]#xref:web/webflux-view.adoc#webflux-view-httpmessagewriter[See equivalent in the Reactive stack]#
|
||||
|
||||
The `MappingJackson2JsonView` uses the Jackson library's `ObjectMapper` to render the response
|
||||
The `JacksonJsonView` uses the Jackson library's `JsonMapper` to render the response
|
||||
content as JSON. By default, the entire contents of the model map (with the exception of
|
||||
framework-specific classes) are encoded as JSON. For cases where the contents of the
|
||||
map need to be filtered, you can specify a specific set of model attributes to encode
|
||||
|
|
@ -18,17 +18,17 @@ by using the `modelKeys` property. You can also use the `extractValueFromSingleK
|
|||
property to have the value in single-key models extracted and serialized directly rather
|
||||
than as a map of model attributes.
|
||||
|
||||
You can customize JSON mapping as needed by using Jackson's provided
|
||||
annotations. When you need further control, you can inject a custom `ObjectMapper`
|
||||
through the `ObjectMapper` property, for cases where you need to provide custom JSON
|
||||
serializers and deserializers for specific types.
|
||||
You can customize JSON mapping as needed by using Jackson's provided annotations. When
|
||||
you need further control, you can inject a custom `JsonMapper` through the `JsonMapper`
|
||||
or `JsonMapper.Builder` constructor parameters, for cases where you need to provide
|
||||
custom JSON serializers and deserializers for specific types.
|
||||
|
||||
|
||||
[[mvc-view-xml-mapping]]
|
||||
== Jackson-based XML Views
|
||||
[.small]#xref:web/webflux-view.adoc#webflux-view-httpmessagewriter[See equivalent in the Reactive stack]#
|
||||
|
||||
`MappingJackson2XmlView` uses the
|
||||
`JacksonXmlView` uses the
|
||||
{jackson-github-org}/jackson-dataformat-xml[Jackson XML extension's] `XmlMapper`
|
||||
to render the response content as XML. If the model contains multiple entries, you should
|
||||
explicitly set the object to be serialized by using the `modelKey` bean property. If the
|
||||
|
|
@ -36,5 +36,5 @@ model contains a single entry, it is serialized automatically.
|
|||
|
||||
You can customize XML mapping as needed by using JAXB or Jackson's provided
|
||||
annotations. When you need further control, you can inject a custom `XmlMapper`
|
||||
through the `ObjectMapper` property, for cases where custom XML
|
||||
you need to provide serializers and deserializers for specific types.
|
||||
created via `XmlMapper.Builder` for cases where custom XML you need to provide
|
||||
serializers and deserializers for specific types.
|
||||
|
|
|
|||
|
|
@ -42,25 +42,25 @@ This converter requires a `Marshaller` and `Unmarshaller` before it can be used.
|
|||
You can inject these through constructor or bean properties.
|
||||
By default, this converter supports `text/xml` and `application/xml`.
|
||||
|
||||
| `MappingJackson2HttpMessageConverter`
|
||||
| An `HttpMessageConverter` implementation that can read and write JSON by using Jackson's `ObjectMapper`.
|
||||
| `JacksonJsonHttpMessageConverter`
|
||||
| An `HttpMessageConverter` implementation that can read and write JSON by using Jackson's `JsonMapper`.
|
||||
You can customize JSON mapping as needed through the use of Jackson's provided annotations.
|
||||
When you need further control (for cases where custom JSON serializers/deserializers need to be provided for specific types), you can inject a custom `ObjectMapper` through the `ObjectMapper` property.
|
||||
By default, this converter supports `application/json`. This requires the `com.fasterxml.jackson.core:jackson-databind` dependency.
|
||||
When you need further control (for cases where custom JSON serializers/deserializers need to be provided for specific types), you can inject a custom `JsonMapper` through the `JsonMapper` or `JsonMapper.Builder ` constructor parameters.
|
||||
By default, this converter supports `application/json`. This requires the `tools.jackson.core:jackson-databind` dependency.
|
||||
|
||||
| `MappingJackson2XmlHttpMessageConverter`
|
||||
| `JacksonXmlHttpMessageConverter`
|
||||
| An `HttpMessageConverter` implementation that can read and write XML by using {jackson-github-org}/jackson-dataformat-xml[Jackson XML] extension's `XmlMapper`.
|
||||
You can customize XML mapping as needed through the use of JAXB or Jackson's provided annotations.
|
||||
When you need further control (for cases where custom XML serializers/deserializers need to be provided for specific types), you can inject a custom `XmlMapper` through the `ObjectMapper` property.
|
||||
By default, this converter supports `application/xml`. This requires the `com.fasterxml.jackson.dataformat:jackson-dataformat-xml` dependency.
|
||||
When you need further control (for cases where custom XML serializers/deserializers need to be provided for specific types), you can inject a custom `XmlMapper` through the `JsonMapper` or `JsonMapper.Builder` constructor parameters.
|
||||
By default, this converter supports `application/xml`. This requires the `tools.jackson.dataformat:jackson-dataformat-xml` dependency.
|
||||
|
||||
| `KotlinSerializationJsonHttpMessageConverter`
|
||||
| An `HttpMessageConverter` implementation that can read and write JSON using `kotlinx.serialization`.
|
||||
This converter is not configured by default, as this conflicts with Jackson.
|
||||
Developers must configure it as an additional converter ahead of the Jackson one.
|
||||
|
||||
| `MappingJackson2CborHttpMessageConverter`
|
||||
| `com.fasterxml.jackson.dataformat:jackson-dataformat-cbor`
|
||||
| `JacksonCborHttpMessageConverter`
|
||||
| `tools.jackson.dataformat:jackson-dataformat-cbor`
|
||||
|
||||
| `SourceHttpMessageConverter`
|
||||
| An `HttpMessageConverter` implementation that can read and write `javax.xml.transform.Source` from the HTTP request and response.
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ import java.util.List;
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.ApplicationWebConfiguration;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.AbstractJacksonHttpMessageConverter ;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
|
@ -34,7 +34,7 @@ class AccountControllerIntegrationTests {
|
|||
|
||||
AccountControllerIntegrationTests(@Autowired WebApplicationContext wac) {
|
||||
this.mockMvc = MockMvcTester.from(wac).withHttpMessageConverters(
|
||||
List.of(wac.getBean(AbstractJackson2HttpMessageConverter.class)));
|
||||
List.of(wac.getBean(AbstractJacksonHttpMessageConverter.class)));
|
||||
}
|
||||
|
||||
// ...
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
||||
import org.springframework.web.servlet.view.json.JacksonJsonView;
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
// tag::snippet[]
|
||||
|
|
@ -30,7 +30,7 @@ public class FreeMarkerConfiguration implements WebMvcConfigurer {
|
|||
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
registry.enableContentNegotiation(new MappingJackson2JsonView());
|
||||
registry.enableContentNegotiation(new JacksonJsonView());
|
||||
registry.freeMarker().cache(false);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigviewresolvers;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
||||
import org.springframework.web.servlet.view.json.JacksonJsonView;
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
// tag::snippet[]
|
||||
|
|
@ -28,7 +28,7 @@ public class WebConfiguration implements WebMvcConfigurer {
|
|||
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
registry.enableContentNegotiation(new MappingJackson2JsonView());
|
||||
registry.enableContentNegotiation(new JacksonJsonView());
|
||||
registry.jsp();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.conv
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.ApplicationWebConfiguration
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
|
||||
import org.springframework.http.converter.AbstractJacksonHttpMessageConverter
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import org.springframework.web.context.WebApplicationContext
|
||||
|
|
@ -30,7 +30,7 @@ import org.springframework.web.context.WebApplicationContext
|
|||
class AccountControllerIntegrationTests(@Autowired wac: WebApplicationContext) {
|
||||
|
||||
private val mockMvc = MockMvcTester.from(wac).withHttpMessageConverters(
|
||||
listOf(wac.getBean(AbstractJackson2HttpMessageConverter::class.java)))
|
||||
listOf(wac.getBean(AbstractJacksonHttpMessageConverter::class.java)))
|
||||
|
||||
// ...
|
||||
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ import org.springframework.context.annotation.Configuration
|
|||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView
|
||||
import org.springframework.web.servlet.view.json.JacksonJsonView
|
||||
|
||||
// tag::snippet[]
|
||||
@Configuration
|
||||
class FreeMarkerConfiguration : WebMvcConfigurer {
|
||||
|
||||
override fun configureViewResolvers(registry: ViewResolverRegistry) {
|
||||
registry.enableContentNegotiation(MappingJackson2JsonView())
|
||||
registry.enableContentNegotiation(JacksonJsonView())
|
||||
registry.freeMarker().cache(false)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,13 +21,13 @@ package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigviewresolvers
|
|||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView
|
||||
import org.springframework.web.servlet.view.json.JacksonJsonView
|
||||
|
||||
// tag::snippet[]
|
||||
@Configuration
|
||||
class WebConfiguration : WebMvcConfigurer {
|
||||
override fun configureViewResolvers(registry: ViewResolverRegistry) {
|
||||
registry.enableContentNegotiation(MappingJackson2JsonView())
|
||||
registry.enableContentNegotiation(JacksonJsonView())
|
||||
registry.jsp()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
<mvc:view-resolvers>
|
||||
<mvc:content-negotiation>
|
||||
<mvc:default-views>
|
||||
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
|
||||
<bean class="org.springframework.web.servlet.view.json.JacksonJsonView"/>
|
||||
</mvc:default-views>
|
||||
</mvc:content-negotiation>
|
||||
<mvc:freemarker cache-views="false"/>
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
<mvc:view-resolvers>
|
||||
<mvc:content-negotiation>
|
||||
<mvc:default-views>
|
||||
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
|
||||
<bean class="org.springframework.web.servlet.view.json.JacksonJsonView"/>
|
||||
</mvc:default-views>
|
||||
</mvc:content-negotiation>
|
||||
<mvc:jsp/>
|
||||
|
|
|
|||
Loading…
Reference in New Issue