Browse Source

Move JsonComponent docs to general Jackson section

Closes gh-30405
pull/30472/head
Andy Wilkinson 4 years ago
parent
commit
8646ac4215
  1. 5
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties
  2. 22
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/json.adoc
  3. 2
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/reactive.adoc
  4. 22
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc
  5. 2
      spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyJsonComponent.java
  6. 2
      spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyObject.java
  7. 2
      spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyJsonComponent.java
  8. 2
      spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyObject.java
  9. 2
      spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyJsonComponent.kt
  10. 2
      spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyObject.kt
  11. 2
      spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyJsonComponent.kt
  12. 2
      spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyObject.kt

5
spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties

@ -1012,4 +1012,7 @@ dependency-versions-coordinates=appendix.dependency-versions.coordinates @@ -1012,4 +1012,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

22
spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/json.adoc

@ -18,6 +18,28 @@ Several configuration properties are provided for <<howto#howto.spring-mvc.custo @@ -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.

2
spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/reactive.adoc

@ -75,7 +75,7 @@ If you need to add or customize codecs, you can create a custom `CodecCustomizer @@ -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>>.

22
spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc

@ -75,28 +75,6 @@ You can also override default converters in the same way. @@ -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`.

2
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/json/MyJsonComponent.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyJsonComponent.java

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* 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;

2
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/json/MyObject.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyObject.java

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.web.servlet.springmvc.json;
package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers;
class MyObject {

2
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/json/object/MyJsonComponent.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyJsonComponent.java

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* 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;

2
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/json/object/MyObject.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyObject.java

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* 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 {

2
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/json/MyJsonComponent.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyJsonComponent.kt

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* 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.JsonParser

2
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/json/MyObject.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/MyObject.kt

@ -14,6 +14,6 @@ @@ -14,6 +14,6 @@
* 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)

2
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/json/object/MyJsonComponent.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyJsonComponent.kt

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* 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.JsonParser

2
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/json/object/MyObject.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/json/jackson/customserializersanddeserializers/object/MyObject.kt

@ -14,6 +14,6 @@ @@ -14,6 +14,6 @@
* 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)
Loading…
Cancel
Save