65 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| [[features.json]]
 | |
| == JSON
 | |
| Spring Boot provides integration with three JSON mapping libraries:
 | |
| 
 | |
| - Gson
 | |
| - Jackson
 | |
| - JSON-B
 | |
| 
 | |
| Jackson is the preferred and default library.
 | |
| 
 | |
| 
 | |
| 
 | |
| [[features.json.jackson]]
 | |
| === Jackson
 | |
| Auto-configuration for Jackson is provided and Jackson is part of `spring-boot-starter-json`.
 | |
| When Jackson is on the classpath an `ObjectMapper` bean is automatically configured.
 | |
| Several configuration properties are provided for <<howto#howto.spring-mvc.customize-jackson-objectmapper,customizing the configuration of the `ObjectMapper`>>.
 | |
| 
 | |
| 
 | |
| 
 | |
| [[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.jackson.mixins]]
 | |
| ==== Mixins
 | |
| Jackson has support for mixins that can be used to mix additional annotations into those already declared on a target class.
 | |
| Spring Boot's Jackson auto-configuration will scan your application's packages for classes annotated with `@JsonMixin` and register them with the auto-configured `ObjectMapper`.
 | |
| The registration is performed by Spring Boot's `JsonMixinModule`.
 | |
| 
 | |
| 
 | |
| 
 | |
| [[features.json.gson]]
 | |
| === Gson
 | |
| Auto-configuration for Gson is provided.
 | |
| When Gson is on the classpath a `Gson` bean is automatically configured.
 | |
| Several `+spring.gson.*+` configuration properties are provided for customizing the configuration.
 | |
| To take more control, one or more `GsonBuilderCustomizer` beans can be used.
 | |
| 
 | |
| 
 | |
| 
 | |
| [[features.json.json-b]]
 | |
| === JSON-B
 | |
| Auto-configuration for JSON-B is provided.
 | |
| When the JSON-B API and an implementation are on the classpath a `Jsonb` bean will be automatically configured.
 | |
| The preferred JSON-B implementation is Apache Johnzon for which dependency management is provided.
 |