57. springboot XML-based RESTful service—XML format RESTful response and replacement of the default JSON library (support for three JSON libraries: Json, Gson, JSON-B, all Json parsers)

XML format RESTful response and replacing the default JSON library

Spring Boot has built-in support for the following three JSON libraries:

- Jackson (default, included in the spring-boot-starter-web package, and dependent on jackson)
- Gson (Google)
-JSON-B

They are all Json parsers, and their functions are to convert data into json format.
They are all parsers used to generate JSON responses and process request parameters in JSON format.

XML-based RESTful service

▲ Spring Boot provides content negotiation support. By default (and priority), the corresponding response will be generated based on the Accept request header.

 If the Accept request header is application/json, a JSON response will be generated.
 If the Accept request header is application/xml, an XML response will be generated.

 Controller methods just return data (Java objects, collections) --- implemented through HttpMessageConverter -------> JSON or XML

▲ Set spring.mvc.contentnegotiation.favor-parameter to true to enable the use of additional format request parameters to specify the response type.

▲ It is also possible to force the use of URL suffix to specify the response type (this method is obsolete).

In other words, whether you are generating a JSON response or an XML response, you need a specific converter (HttpMessageConverter implementation class)

Spring Boot’s starter-web relies on Jackson – a powerful JSON library by default.

▲ In order for Spring Boot to convert the method returned by the controller into an XML response, the corresponding XML formatter (HttpMessageConverter implementation class) must be added

 The reason why Spring Boot can convert the methods returned by the controller into a JSON response by default is because it relies on the Jackson library by default.
 
▲Spring Boot has built-in support for two XML libraries to allow RESTful services to generate XML responses:
   - XML binding using jackson-dataformat-xml.
   - Use the JAXB XML binding that comes with the JDK.

 jackson-dataformat-xml will automatically serialize Java objects into XML elements with the same name,
 for example
 Book objects are serialized into <Book.../> elements,
 Collection objects are serialized into <Collection.../> elements,
 A sequence of List objects is serialized into <List.../> elements.

 JAXB requires the use of @XmlRootElement and @XmlElement annotations to specify which XML element the Java object is serialized into.
 Therefore, JAXB is more troublesome to handle. It is recommended to use XML binding based on jackson-dataformat-xml.

 [Summary] There is no difference between generating XML response and generating JSON response program.
        The controller method of the program always returns only data (collections, Java objects),
        In the end, Spring is used to convert the data into a JSON response or XML response through (HttpMessageConverter).

  The only difference is that Spring Boot's spring-boot-starter-web relies on the JSON conversion library (Jackson) by default.
                But Spring Boot does not rely on the XML converter by default, so you need to add it manually in pom.xml.

Code demo

Previous article: 56. springboot —— RESTful service and RESTful interface design
The code is modified based on the previous article.

The demonstration response is in xml format, and the corresponding response is generated by modifying the Accept request header when making a request.
If the Accept request header is application/xml, an XML response will be generated.

Just add this dependency

 <!-- Add Jackson format XML for conversion between objects and XML -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

The effect is as shown in the figure:
The Accept request header is xml, and the response format is xml.

Configuration file:
As shown in the figure: Enable additional format request parameters to specify the response type by adding configuration.
Just add ?format=json/xml response type after the access method

#Enable additional format request parameters to specify the response type.
spring.mvc.contentnegotiation.favor-parameter=true

Gson support

They are all Json parsers, and their functions are to convert data into json format.
They are all parsers used to generate JSON responses and process request parameters in JSON format.

If you want to use Gson as the JSON parsing library, just exclude spring-boot-starter-json from the dependency configuration and add the Gson dependency library.

As long as it detects that the Gson library is included in the class loading path, Spring Boot will automatically configure a Gson Bean, which is responsible for providing automatic configuration support for Gson.

Spring Boot provides the following commonly used configuration properties for Gson:

spring.gson.pretty-printing: Whether to format the JSON string.
spring.gson.date-format: Specifies the serialization format of the date. For example, yyyy-MM-dd.
spring.gson.serialize-nulls: Specifies whether to serialize null values.
spring.gson.disable-html-escaping: Specifies whether to disable HTML escaping.
spring.gson.disable-inner-class-serialization: Specifies whether to disable inner class serialization.
spring.gson.enable-complex-map-key-serialization: Specifies whether to enable serialization for composite Map keys.

Code demo Gson

Exclude the original json and use the gson library

Just add this configuration in application.properties configuration file

#Enable additional format request parameters to specify the response type.
spring.mvc.contentnegotiation.favor-parameter=true
#Specify the serialization format of the date
spring.gson.date-format=yyyy-MM-dd
#Output well-formatted json data. When deploying the project, set it to false to save bandwidth.
spring.gson.pretty-printing=true
#Specify whether to enable serialization for composite Map keys
spring.gson.enable-complex-map-key-serialization=true

test:
It is no different from using json, it is to convert the data into json format.
They are all Json parsers, and their functions are to convert data into json format.

JSON-B support

They are all Json parsers, and their functions are to convert data into json format.
They are all parsers used to generate JSON responses and process request parameters in JSON format.

If you want to use JSON-B as the JSON parsing library, just exclude spring-boot-starter-json from the dependency configuration.
And just add the API and implementation library of JSON-B. 3 JAR packages are required:
– JSON API
– JSON Bind API
– johnzon Jsonb

As long as it detects that the class loading path contains the JSON-B API and implementation library, Spring Boot will automatically configure a Jsonb Bean.

[Conclusion] If you want to generate a JSON response and process request parameters in JSON format, you can actually use JACKSON recommended by Spring Boot.
        What is taught here is that you can change the JSON parser, but it is not recommended that you do this.

Code demo:

Change the dependent library to json b

It is no different from the above, both can realize the conversion of json format, but the conversion at this time is supported by the JSON-B parser.