Merge branch '2.7.x'
This commit is contained in:
@@ -1003,4 +1003,7 @@ dependency-versions-coordinates=appendix.dependency-versions.coordinates
|
||||
dependency-versions-properties=appendix.dependency-versions.properties
|
||||
|
||||
dependency-versions.coordinates=appendix.dependency-versions.coordinates
|
||||
dependency-versions.properties=appendix.dependency-versions.properties
|
||||
dependency-versions.properties=appendix.dependency-versions.properties
|
||||
|
||||
# gh-30405
|
||||
web.servlet.spring-mvc.json=features.json.jackson.custom-serializers-and-deserializers
|
||||
@@ -18,6 +18,28 @@ Several configuration properties are provided for <<howto#howto.spring-mvc.custo
|
||||
|
||||
|
||||
|
||||
[[features.json.jackson.custom-serializers-and-deserializers]]
|
||||
==== Custom Serializers and Deserializers
|
||||
If you use Jackson to serialize and deserialize JSON data, you might want to write your own `JsonSerializer` and `JsonDeserializer` classes.
|
||||
Custom serializers are usually https://github.com/FasterXML/jackson-docs/wiki/JacksonHowToCustomSerializers[registered with Jackson through a module], but Spring Boot provides an alternative `@JsonComponent` annotation that makes it easier to directly register Spring Beans.
|
||||
|
||||
You can use the `@JsonComponent` annotation directly on `JsonSerializer`, `JsonDeserializer` or `KeyDeserializer` implementations.
|
||||
You can also use it on classes that contain serializers/deserializers as inner classes, as shown in the following example:
|
||||
|
||||
include::code:MyJsonComponent[]
|
||||
|
||||
All `@JsonComponent` beans in the `ApplicationContext` are automatically registered with Jackson.
|
||||
Because `@JsonComponent` is meta-annotated with `@Component`, the usual component-scanning rules apply.
|
||||
|
||||
Spring Boot also provides {spring-boot-module-code}/jackson/JsonObjectSerializer.java[`JsonObjectSerializer`] and {spring-boot-module-code}/jackson/JsonObjectDeserializer.java[`JsonObjectDeserializer`] base classes that provide useful alternatives to the standard Jackson versions when serializing objects.
|
||||
See {spring-boot-module-api}/jackson/JsonObjectSerializer.html[`JsonObjectSerializer`] and {spring-boot-module-api}/jackson/JsonObjectDeserializer.html[`JsonObjectDeserializer`] in the Javadoc for details.
|
||||
|
||||
The example above can be rewritten to use `JsonObjectSerializer`/`JsonObjectDeserializer` as follows:
|
||||
|
||||
include::code:object/MyJsonComponent[]
|
||||
|
||||
|
||||
|
||||
[[features.json.gson]]
|
||||
=== Gson
|
||||
Auto-configuration for Gson is provided.
|
||||
|
||||
@@ -75,7 +75,7 @@ If you need to add or customize codecs, you can create a custom `CodecCustomizer
|
||||
|
||||
include::code:MyCodecsConfiguration[]
|
||||
|
||||
You can also leverage <<web#web.servlet.spring-mvc.json,Boot's custom JSON serializers and deserializers>>.
|
||||
You can also leverage <<features#features.json.jackson.custom-serializers-and-deserializers,Boot's custom JSON serializers and deserializers>>.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -75,28 +75,6 @@ You can also override default converters in the same way.
|
||||
|
||||
|
||||
|
||||
[[web.servlet.spring-mvc.json]]
|
||||
==== Custom JSON Serializers and Deserializers
|
||||
If you use Jackson to serialize and deserialize JSON data, you might want to write your own `JsonSerializer` and `JsonDeserializer` classes.
|
||||
Custom serializers are usually https://github.com/FasterXML/jackson-docs/wiki/JacksonHowToCustomSerializers[registered with Jackson through a module], but Spring Boot provides an alternative `@JsonComponent` annotation that makes it easier to directly register Spring Beans.
|
||||
|
||||
You can use the `@JsonComponent` annotation directly on `JsonSerializer`, `JsonDeserializer` or `KeyDeserializer` implementations.
|
||||
You can also use it on classes that contain serializers/deserializers as inner classes, as shown in the following example:
|
||||
|
||||
include::code:MyJsonComponent[]
|
||||
|
||||
All `@JsonComponent` beans in the `ApplicationContext` are automatically registered with Jackson.
|
||||
Because `@JsonComponent` is meta-annotated with `@Component`, the usual component-scanning rules apply.
|
||||
|
||||
Spring Boot also provides {spring-boot-module-code}/jackson/JsonObjectSerializer.java[`JsonObjectSerializer`] and {spring-boot-module-code}/jackson/JsonObjectDeserializer.java[`JsonObjectDeserializer`] base classes that provide useful alternatives to the standard Jackson versions when serializing objects.
|
||||
See {spring-boot-module-api}/jackson/JsonObjectSerializer.html[`JsonObjectSerializer`] and {spring-boot-module-api}/jackson/JsonObjectDeserializer.html[`JsonObjectDeserializer`] in the Javadoc for details.
|
||||
|
||||
The example above can be rewritten to use `JsonObjectSerializer`/`JsonObjectDeserializer` as follows:
|
||||
|
||||
include::code:object/MyJsonComponent[]
|
||||
|
||||
|
||||
|
||||
[[web.servlet.spring-mvc.message-codes]]
|
||||
==== MessageCodesResolver
|
||||
Spring MVC has a strategy for generating error codes for rendering error messages from binding errors: `MessageCodesResolver`.
|
||||
|
||||
Reference in New Issue
Block a user