Spring content negotiation
-
Upload
rahmanmds1 -
Category
Technology
-
view
96 -
download
0
Transcript of Spring content negotiation
![Page 1: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/1.jpg)
Handling multiple data Handling multiple data representation with Spring representation with Spring
MVCMVC
![Page 2: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/2.jpg)
AgendaAgenda
Introduction How content negotiation works? Spring MVC default behavior and
Customizing content negotiation. Quick demo References.
![Page 3: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/3.jpg)
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
![Page 4: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/4.jpg)
Introduction (2)Introduction (2)
When @ResponseBody annotation is used view based rendering is totally bypassed.
![Page 5: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/5.jpg)
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
![Page 6: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/6.jpg)
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.)
![Page 7: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/7.jpg)
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);
}
}
![Page 8: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/8.jpg)
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>
![Page 9: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/9.jpg)
Git-Hub and other LinksGit-Hub and other Links
Git link for code and SOAPUI project : https://github.com/rahmanmd1/Spring-Rest-Stack
![Page 10: Spring content negotiation](https://reader036.fdocuments.us/reader036/viewer/2022070520/58f264c81a28aba3378b45b1/html5/thumbnails/10.jpg)
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