Spring content negotiation

Preview:

Citation preview

Handling multiple data Handling multiple data representation with Spring representation with Spring

MVCMVC

AgendaAgenda

Introduction How content negotiation works? Spring MVC default behavior and

Customizing content negotiation. Quick demo References.

Introduction (1)Introduction (1) Controller handles all HTTP method :

GET, PUT, DELETE, and POST. @PathVariable annotation enables

controllers to handle requests for paramterized URLS.

We can represent resources in varierty of ways. Content-NegotiatingViewResolver is used to represent suited resources to consumer

Introduction (2)Introduction (2)

When @ResponseBody annotation is used view based rendering is totally bypassed.

How content negotiation works How content negotiation works (1)(1)

How to determine what kind of resource representation the client wants ? [default]

Priority sequence URL extension Query param HTTP accept Header

Spring MVC default behavior and Spring MVC default behavior and Customizing content negotiation. Customizing content negotiation. (1)(1)

▫ Default behavior can be changed by ContentNegotiationManager. Few things we can consider:

• Via a request parameter Example : http://api-dev.ipfactory.cerner.corp/rest/solution-change?

format=json

• Map URL extension Example : http://api-dev.ipfactory.cerner.corp/rest/solution-change.json

• Specify default content-type.• Ignore request Accept header. (accept header can’t be

trusted as it is messed up specially with browser.)• Use JAF ( java activation framework.)

Spring MVC default behavior and Spring MVC default behavior and Customizing content negotiation. Customizing content negotiation. (2)(2)Congiguration option #1 java configuration@Configuration

@EnableWebMvc

public class WebConfig extends WebMvcConfigurerAdapter {

@Override

public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

configurer.favorPathExtension(false).

favorParameter(true).

parameterName("mediaType").

ignoreAcceptHeader(true).

useJaf(false).

defaultContentType(MediaType.APPLICATION_JSON).

mediaType("xml", MediaType.APPLICATION_XML).

mediaType("json", MediaType.APPLICATION_JSON);

}

}

Spring MVC default behavior and Spring MVC default behavior and Customizing content negotiation. Customizing content negotiation. (2)(2)Congiguration option #2 xml configuration

<bean id="contentNegotiationManager"

class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">

<property name="favorPathExtension" value="false" />

<property name="favorParameter" value="true" />

<property name="parameterName" value="mediaType" />

<property name="ignoreAcceptHeader" value="true"/>

<property name="useJaf" value="false"/>

<property name="defaultContentType" value="application/json" />

<property name="mediaTypes">

<map>

<entry key="json" value="application/json" />

<entry key="xml" value="application/xml" />

</map>

</property>

</bean>

Git-Hub and other LinksGit-Hub and other Links

Git link for code and SOAPUI project : https://github.com/rahmanmd1/Spring-Rest-Stack

https://dzone.com/articles/content-negotiation-usinghttps://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvchttps://spring.io/blog/2013/06/03/content-negotiation-using-views/http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/http://websystique.com/springmvc/spring-4-mvc-rest-service-example-using-restcontroller/

ReferencesReferences

Recommended