Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Content Negotiation Configuration

In this section, you will learn about configuring content negotiation in Spring MVC.

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

Content Negotiation Configuration

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 checking file extension for each type of URI. For example: http://www,roseindia.net/java/tutorial_beginner.pdf requesting PDF representation and http://www,roseindia.net/java/tutorial_beginner.xml requesting XML representation.

  • By checking Accept HTTP request header . For example, a request for http://www.roseindia.net/java/tutorial_beginner with an Accept header set to 'application/pdf' requests a PDF representation.

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.

Advertisements

Share on Google+Share on Google+

Content Negotiation Configuration

Posted on: February 7, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Content Negotiation Configuration  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads