2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# Integrate Fastjson2 in SpringFramework
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# 0. Download
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Fastjson2 adopts a multi-module structure design, and the support for frameworks such as SpringFramework is now
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:11:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								independent in the `extension`  dependency.
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`Maven` : 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```xml
							 
						 
					
						
							
								
									
										
										
										
											2023-01-05 08:32:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< dependency >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < groupId > com.alibaba.fastjson2< / groupId > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < artifactId > fastjson2-extension-spring5< / artifactId > 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-31 12:11:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    < version > 2.0.57< / version > 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-05 08:32:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / dependency >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								or
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< dependency >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < groupId > com.alibaba.fastjson2< / groupId > 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-05 08:32:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    < artifactId > fastjson2-extension-spring6< / artifactId > 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-31 12:11:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    < version > 2.0.57< / version > 
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / dependency >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`Gradle` : 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```groovy
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								dependencies {
							 
						 
					
						
							
								
									
										
										
										
											2025-03-31 12:11:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.57'
							 
						 
					
						
							
								
									
										
										
										
											2023-01-05 08:32:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								or
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								dependencies {
							 
						 
					
						
							
								
									
										
										
										
											2025-03-31 12:11:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.57'
							 
						 
					
						
							
								
									
										
										
										
											2023-01-05 08:32:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2023-01-14 15:31:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								>  After version 2.0.23, in order to be compatible with Spring 5.x / 6.x, different versions are independently opened with different dependency packages.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								>  If using a version earlier than 2.0.23, please refer to:
  
						 
					
						
							
								
									
										
										
										
											2023-01-05 08:32:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`Maven` : 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```xml
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< dependency >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < groupId > com.alibaba.fastjson2< / groupId > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < artifactId > fastjson2-extension< / artifactId > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < version > 2.0.x< / version > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / dependency >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`Gradle` : 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```groovy
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								dependencies {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    implementation 'com.alibaba.fastjson2:fastjson2-extension:2.0.x'
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# 1. Configuration
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Fastjson2 has redesigned the behavior of serialization and deserialization, so `FastJsonConfig`  will also be re-adapted.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.config.FastJsonConfig` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Attributes**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								param | Type | Desc
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---- | ---- | ----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								charset | Charset | The specified character set, default UTF-8
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								dateFormat | String | The specified date format, default yyyy-MM-dd HH:mm:ss
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								writerFilters | Filter[] | Configure serialization filters
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								writerFeatures | JSONWriter.Feature[] | Configure the specified behavior of serialization. For more configuration, see [Features ](features_en.md )
							 
						 
					
						
							
								
									
										
										
										
											2022-05-26 21:23:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								readerFilters | Filter[] | Configure deserialization filters
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								readerFeatures | JSONReader.Feature[] | Configure the specified behavior of deserialization. For more configuration, see [Features ](features_en.md )
							 
						 
					
						
							
								
									
										
										
										
											2022-05-25 16:04:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								jsonb | boolean | Use JSONB for serialization and deserialization, the default is false
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								symbolTable | JSONB.SymbolTable | JSONB serialization and deserialization symbol table, only valid when using JSONB
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# 2. Integrate Fastjson2 in Spring Web MVC
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In Fastjson2, `FastJsonHttpMessageConverter`  and `FastJsonJsonView`  can also be used to provide a better performance
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								experience for Web applications built with Spring MVC.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 2.1  Spring Web MVC Converter
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use `FastJsonHttpMessageConverter`  to replace Spring MVC's default `HttpMessageConverter`  to improve JSON serialization
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and deserialization speed of `@RestController`  `@ResponseBody`  `@RequestBody`  annotations.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-08-25 23:05:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Before Spring 5 Example**:
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableWebMvc  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Override 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public void configureMessageConverters(List< HttpMessageConverter < ? > > converters) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //custom configuration...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonConfig config = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converter.setFastJsonConfig(config);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converter.setDefaultCharset(StandardCharsets.UTF_8);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converters.add(0, converter);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-08-25 23:05:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Starting from Spring 5.0, `WebMvcConfigurerAdapter`  has been deprecated, you can directly implement the `WebMvcConfigurer`  interface without using this adapter.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**After Spring 5 Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableWebMvc  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class CustomWebMvcConfigurer implements WebMvcConfigurer {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Override 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public void configureMessageConverters(List< HttpMessageConverter < ? > > converters) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //custom configuration...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonConfig config = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converter.setFastJsonConfig(config);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converter.setDefaultCharset(StandardCharsets.UTF_8);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        converters.add(0, converter);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								## 2.2  Spring Web MVC View
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use `FastJsonJsonView`  to set Spring MVC's default view model resolver to improve the speed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								of `@Controller`  `@ResponseBody`  `ModelAndView`  JSON serialization.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.spring.webservlet.view.FastJsonJsonView` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-08-25 23:05:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**Before Spring 5 Example**:
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableWebMvc  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Override 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public void configureViewResolvers(ViewResolverRegistry registry) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonJsonView fastJsonJsonView = new FastJsonJsonView();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //custom configuration...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //FastJsonConfig config = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //config.set...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //fastJsonJsonView.setFastJsonConfig(config);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        registry.enableContentNegotiation(fastJsonJsonView);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-08-25 23:05:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Starting from Spring 5.0, `WebMvcConfigurerAdapter`  has been deprecated, you can directly implement the `WebMvcConfigurer`  interface without using this adapter.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**After Spring 5 Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableWebMvc  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class CustomWebMvcConfigurer implements WebMvcConfigurer {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Override 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public void configureViewResolvers(ViewResolverRegistry registry) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonJsonView fastJsonJsonView = new FastJsonJsonView();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //custom configuration...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //FastJsonConfig config = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //config.set...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //fastJsonJsonView.setFastJsonConfig(config);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        registry.enableContentNegotiation(fastJsonJsonView);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								>  Reference: Spring Framework official documentation Spring Web MVC section, [For more configuration](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-config).
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# 3. Integrate Fastjson2 in Spring Web Socket
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In Fastjson2, Spring WebSocket is also supported, which can be configured using `FastjsonSockJsMessageCodec` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.spring.websocket.sockjs.FastjsonSockJsMessageCodec` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Component  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableWebSocket  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Resource 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    WebSocketHandler handler;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //custom configuration...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //FastjsonSockJsMessageCodec messageCodec = new FastjsonSockJsMessageCodec();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //FastJsonConfig config = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //config.set...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //messageCodec.setFastJsonConfig(config);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        registry.addHandler(handler, "/sockjs").withSockJS().setMessageCodec(new FastjsonSockJsMessageCodec());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								>  Reference: Spring Framework official documentation Spring Web Socket section, [For more configuration](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket).
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# 4. Integrate Fastjson2 in Spring Data Redis
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In Fastjson2, you can also use `GenericFastJsonRedisSerializer`  or `FastJsonRedisSerializer`  to provide a better
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								performance experience for Spring Data Redis.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 4.1 Generic Redis Serializer
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use `GenericFastJsonRedisSerializer`  as the `RedisSerializer`  of `RedisTemplate`  to improve JSON serialization and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								deserialization speed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class RedisConfiguration {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Bean 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        RedisTemplate redisTemplate = new RedisTemplate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        redisTemplate.setConnectionFactory(redisConnectionFactory);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);//Set the default Serialize, including keySerializer &  valueSerializer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //redisTemplate.setKeySerializer(fastJsonRedisSerializer);//Only set keySerializer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //redisTemplate.setValueSerializer(fastJsonRedisSerializer);//Only set valueSerializer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return redisTemplate;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 4.2 Customized Redis Serializer
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Usually, `GenericFastJsonRedisSerializer`  can be used for most scenarios. If you want to define a specific type
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								of `RedisTemplate` , you can use `FastJsonRedisSerializer`  instead of `GenericFastJsonRedisSerializer`  , the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								configuration is similar.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.spring.data.redis.FastJsonRedisSerializer` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class RedisConfiguration {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Bean 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        RedisTemplate redisTemplate = new RedisTemplate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        redisTemplate.setConnectionFactory(redisConnectionFactory);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(User.class);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return redisTemplate;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 4.3 JSONB Redis Serializer
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you plan to use JSONB as an object serialization/deserialization method and have higher serialization speed
							 
						 
					
						
							
								
									
										
										
										
											2022-06-05 00:12:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								requirements, you can configure the `jsonb`  parameter, which is a new support in fastjson 2.0.6, and the configuration
							 
						 
					
						
							
								
									
										
										
										
											2022-05-25 16:04:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								is also very simple.
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class RedisConfiguration {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Bean 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        RedisTemplate redisTemplate = new RedisTemplate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        redisTemplate.setConnectionFactory(redisConnectionFactory);
							 
						 
					
						
							
								
									
										
										
										
											2022-05-25 16:04:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        // GenericFastJsonRedisSerializer use jsonb
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer(true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // FastJsonRedisSerializer use jsonb
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(User.class);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // FastJsonConfig fastJsonConfig = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // fastJsonConfig.setJSONB(true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // fastJsonRedisSerializer.setFastJsonConfig(fastJsonConfig);
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);
							 
						 
					
						
							
								
									
										
										
										
											2022-05-25 16:04:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-05-22 16:00:22 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        return redisTemplate;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								>  Reference: Spring Data Redis official documentation, [For more configuration](https://docs.spring.io/spring-data/redis/docs/current/reference/html/).
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-05-25 16:04:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# 5. Integrate Fastjson2 in Spring Messaging
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In Fastjson2, you can use `MappingFastJsonMessageConverter`  to provide a better performance experience for Spring
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Messaging.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 5.1 JSON Message Converter
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use `MappingFastJsonMessageConverter`  as Spring Cloud Stream or Spring Messaging to speed up message serialization and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								deserialization.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Package**: `com.alibaba.fastjson2.support.spring.messaging.converter.MappingFastJsonMessageConverter` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class StreamConfiguration {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Bean 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @StreamMessageConverter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public MappingFastJsonMessageConverter messageConverter() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return new MappingFastJsonMessageConverter();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## 5.2 JSONB Message Converter
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you plan to use JSONB as the object serialization/deserialization method and have high requirements on serialization
							 
						 
					
						
							
								
									
										
										
										
											2022-06-05 00:12:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								speed, you can configure the `jsonb`  parameter of `FastJsonConfig` , which is a new support in fastjson 2.0.6 version ,
							 
						 
					
						
							
								
									
										
										
										
											2022-05-25 16:04:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								the configuration is also very simple.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								_Note: JSONB only supports serializing the payload of Message to byte[]_
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Example**:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class StreamConfiguration {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Bean 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @StreamMessageConverter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public MappingFastJsonMessageConverter messageConverter() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        MappingFastJsonMessageConverter messageConverter = new MappingFastJsonMessageConverter();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        FastJsonConfig fastJsonConfig = new FastJsonConfig();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        fastJsonConfig.setJSONB(true); // use jsonb
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        messageConverter.setFastJsonConfig(fastJsonConfig);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return messageConverter;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								>  Reference: Spring Messaging official documentation, [For more configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/messaging.html#messaging).