Merge branch '2.7.x'
This commit is contained in:
commit
2e32f800e1
|
@ -1003,4 +1003,7 @@ dependency-versions-coordinates=appendix.dependency-versions.coordinates
|
||||||
dependency-versions-properties=appendix.dependency-versions.properties
|
dependency-versions-properties=appendix.dependency-versions.properties
|
||||||
|
|
||||||
dependency-versions.coordinates=appendix.dependency-versions.coordinates
|
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]]
|
[[features.json.gson]]
|
||||||
=== Gson
|
=== Gson
|
||||||
Auto-configuration for Gson is provided.
|
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[]
|
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]]
|
[[web.servlet.spring-mvc.message-codes]]
|
||||||
==== MessageCodesResolver
|
==== MessageCodesResolver
|
||||||
Spring MVC has a strategy for generating error codes for rendering error messages from binding errors: `MessageCodesResolver`.
|
Spring MVC has a strategy for generating error codes for rendering error messages from binding errors: `MessageCodesResolver`.
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json;
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json;
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers;
|
||||||
|
|
||||||
class MyObject {
|
class MyObject {
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json.object;
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers.object;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json.object;
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers.object;
|
||||||
|
|
||||||
class MyObject {
|
class MyObject {
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator
|
import com.fasterxml.jackson.core.JsonGenerator
|
||||||
import com.fasterxml.jackson.core.JsonParser
|
import com.fasterxml.jackson.core.JsonParser
|
|
@ -14,6 +14,6 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers
|
||||||
|
|
||||||
class MyObject(val name: String = "", val age: Int = 0)
|
class MyObject(val name: String = "", val age: Int = 0)
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json.`object`
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers.`object`
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator
|
import com.fasterxml.jackson.core.JsonGenerator
|
||||||
import com.fasterxml.jackson.core.JsonParser
|
import com.fasterxml.jackson.core.JsonParser
|
|
@ -14,6 +14,6 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.docs.web.servlet.springmvc.json.`object`
|
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers.`object`
|
||||||
|
|
||||||
class MyObject(val name: String = "", val age: Int = 0)
|
class MyObject(val name: String = "", val age: Int = 0)
|
Loading…
Reference in New Issue