RESTful APIs sometimes return responses in different media types (JSON, XML, and so on). The mechanism responsible for choosing the correct media type is known as content negotiation in Spring.
By default, in Spring MVC, the ContentNegotiatingViewResolver
bean will be in charge of resolving the correct content according to the content negotiation policies defined in your application.
You can have a look at ContentNegotiationManagerFactoryBean
to see how these policies are applied within Spring MVC.
Content type can be resolved with the following strategies:
According to the
Accept
header sent by the clientWith a parameter such as
?format=json
With a path extension such as
/myResource.json
or/myResource.xml
You can customize these strategies in your Spring configuration by overriding the configureContentNegotiation()
method of the WebMvcConfigurerAdapter
class.
By default, Spring will use the Accept
header and the path extension.
To enable XML serialization with Spring Boot, you...