In this section, you will learn about configuring content negotiation in Spring MVC.
Content negotiation, introduced in Spring 3.2, resolves the requested media type of the request from the client. Afterward, the response is sent back to the client in the resolved media type.
The available options for determining the requested media type are :
By default, extension of the file in the request URI is checked first and "Accept" header is checked next.
You can configure the content negotiation options by the MVC Java config as shown below :
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.setFavorPathExtension(false).setFavorParameter(true);
}
}
You can configure the content negotiation options by the MVC XML Namespace as shown below :
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> <property name="favorParameter" value="true" /> <property name="mediaTypes" > <value> json=application/json xml=application/xml </value> </property> </bean>
In the above configuration, the element <mvc:annotation-driven> has a attribute content-negotiation-manager that accepts a ContentNegotiationManager which can be created with a ContentNegotiationManagerFactoryBean.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Content Negotiation Configuration
Post your Comment