Spring boot integrated web development

Spring boot integrated web development

The format of json returned by web dependency

Jackson databind is added to this dependency by default

As a JSON generator, there is no need to add additional JSON at this time

The manager can return a section of JSON.

Custom converter:

1. Integrated Gson

Besides Jackson databind, the common json processors include Gson and fastjson,

Gson is google's open source json parsing framework. To use gson, first remove the default Jackson databind, and then add gson dependency

pom file configuration

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

Because of the default Gson automatic conversion class GsonHttpMessageConvertersConfiguration in Spring Boot,

Therefore, after the dependency of Gson is added successfully, Gson can be used directly like Jackson databind. But at Gson

If you want to format date data during conversion, you also need to customize HttpMessageConverter.

@Configuration
public class GsonConfig {
    //Provide an instance of gsonHtppMessageConverter
    @Bean
    GsonHttpMessageConverter gsonHttpMessageConverter(){
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
            GsonBuilder builder=new GsonBuilder();
            //Format date
            builder.setDateFormat("yyyy-MM-dd");
            //Filter protected modified fields.
            builder.excludeFieldsWithModifiers(Modifier.PROTECTED);
            //Create a Gson object, give the json object to the GsonHttpMessageConevter instance and return
            Gson gson=builder.create();
            converter.setGson(gson);
            return converter;
    }
}

2. Integrate fastjson

fastjson is an open source JSON parsing framework of Alibaba. It is currently the fastest open source framework for JSON parsing. fastjson cannot be used immediately after inheritance. It can only be used after providing the corresponding HttpMessageConverter

Add pom file

    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.8.2</version>
</dependency>

HttpMessageConverter for fastjson

/**Customize MyFastJsonConfig to complete the provision of FastJsonHttpMessageConverter Bean
 * @author wuyuhong
 * @date 2021 At 10:56 on April 14
 */
@Configuration
public class MyFastJsonConfig {
    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConever(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        // Some details of JSON parsing process are configured, such as date format, data coding, and whether it is being generated
        //Output class name in JSON, whether to output data with null value, generated JSON format, empty collection output [] instead of
        //Null, empty string output "" instead of null, etc
            config.setDateFormat("yyyy-MM-dd");
            config.setCharset(Charset.forName("UTF-8"));
            config.setSerializerFeatures(
                    SerializerFeature.WriteClassName,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty
            );
            converter.setFastJsonConfig(config);
            return converter;
    }
}

After config configuration, you also need to set the return code, application Properties, otherwise Chinese garbled code

server.servlet.encoding.force-response=true

Another way is to implement the webMvcConfigurer class

//@Configuration
public class GsonConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        // Some details of JSON parsing process are configured, such as date format, data coding, and whether it is being generated
        //Output class name in JSON, whether to output data with null value, generated JSON format, empty collection output [] instead of
        //Null, empty string output "" instead of null, etc
        config.setDateFormat("yyyy-MM-dd");
        config.setCharset(Charset.forName("UTF-8"));
        config.setSerializerFeatures(
                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        converter.setFastJsonConfig(config);
        converters.add(converter);

    }
  
}

. customize the MyWebMvcConfig class and implement the configureMessageConverters method in the WebMvcConfigurer interface

. add the custom fastJsonHttpMessageConverter to the converters
$$
If Gson is used, it can also be configured in this way, but it is not recommended. Because when there is no

Gsonh messenger conv has been released. Spring Boot will provide a GsonHttpMessageConverter, which

When overriding the configureMessageConverters method parameter, there is already an instance of GsonHttpMessageConverter in converters. It is necessary to replace the existing instance of GSO nhttpmessageconverter, which is troublesome. Therefore, it is recommended to provide GsonHttpMessageConverter directly for Gson
c o n f i g u r e M e s s a g e C o n v e r t e r s square method ginseng number c o n v e r t e r s in already through have G s o n H t t p M e s s a g e C o n v e r t e r of real example Yes , need want for change already have of G s o n H t t p M e s s a g e C o n v e r real example , exercise do than relatively hemp Annoyed , place with yes to G s o n , PUSH Recommend straight meet carry Offer G s o n H t t p M e s s a g e C o n v e r t e r The configureMessageConverters method parameter converters already has an instance of GsonHttpMessageConverter. The existing GSO nhttpmessageconverter Er instance needs to be replaced. The operation is cumbersome. Therefore, it is recommended to directly provide GsonHttpMessageConverter for Gson There is already an instance of GsonHttpMessageConverter in the parameter converters of configureMessageConverters method. It is necessary to replace the existing instance of GsonHttpMessageConverter, which is troublesome. Therefore, it is recommended to directly provide GsonHttpMessageConverter for Gson

Keywords: Spring Boot

Added by konrados on Mon, 07 Mar 2022 16:59:05 +0200