Configuring content-negotiation (JSON, XML, and so on)

In this recipe, we will see how to configure the way we want the system to decide which format to render depending upon the client expectations.

Getting ready

We are mostly going to review the XML configuration here. Then, we will test the API with different requests to ensure support is provided to the XML format.

How to do it...

  1. The RequestMappingHandlerAdapter configuration has been altered in dispatcher-context.xml. A contentNegotiationManager property has been added, as well as an xmlConverter bean:
    <bean class="org.sfw.web... method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="xmlConverter"/> <ref bean="jsonConverter"/> </list> </property> ...

Get Spring MVC Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.