When using SpringBoot to reference Eureka, the returned result changes from json to xml.

Today, I quoted Eureka when using Spring Boot, which caused the return result to change from JSON to XML. After some inspection and analysis, the problem was solved. Now I will share it with you.

1. Problem Description
When using Spring Boot to build a microservice architecture, Eureka is usually used as the service discovery and registration center. After introducing Eureka, I suddenly found that the return result of the API changed from the original JSON format to XML format.

2. Problem Analysis
In order to solve this problem, you first need to understand Spring Boot’s default Message Converters mechanism. Spring Boot selects the appropriate message converter based on the Content-Type of the request and response, thereby realizing automatic conversion of request and response data. Common message converters include JSON and XML formats.

When Eureka is introduced, it internally integrates Spring Cloud's RestTemplate by default and uses its own message converter. The default message converter used by RestTemplate is Jaxb2RootElementHttpMessageConverter, which converts Java objects into XML format. Therefore, when we call other microservices through Eureka, the returned data is automatically converted to XML format.

3. Solution
In order to solve the problem of returning results from JSON to XML, there are two options to choose from.

Option 1: Modify the message converter
We can manually modify the message converter and replace the default Jaxb2RootElementHttpMessageConverter with MappingJackson2HttpMessageConverter to convert the return result into JSON format. The specific code is as follows:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof Jaxb2RootElementHttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter());
    }
}

In this way, we replace the default XML converter with a JSON converter. Rerun the project and the results returned when calling the API will be in JSON format.

Solution 2: Use the @JsonIgnoreProperties annotation.
Another solution is to use the @JsonIgnoreProperties annotation on the Java object corresponding to the returned result. This annotation can be used to ignore attributes that do not need to be converted to XML. The specific code is as follows:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyResponse {
    // 属性...
}

After adding the @JsonIgnoreProperties annotation to the MyResponse class, the corresponding properties will be ignored and converted to XML format, that is, the returned result will still be in JSON format.

4. Summary
By modifying the message converter or using the @JsonIgnoreProperties annotation, we can solve the problem of Spring Boot referencing Eureka causing the return result to change from JSON to XML. Choose the appropriate solution based on actual needs.

Guess you like

Origin blog.csdn.net/javamendou/article/details/131593687