Spring MVC 3.1.1 ContentNegotiatingViewResolver - How to use wildcard characters in contentTypes?

1k Views Asked by At

I am about to version my REST Webservice, which is based on the ContentNegotiatingViewResolver in order to allow me to return XML, JSON as well as HTML-Views.

The problem is, I can neither use a wildcard character in the contentType of MappingJacksonJsonView, nor in the contentType of xmlMarshallingView, which are both referenced by the ContentNegotiatingViewResolver. But I need to define these contentTypes in a generic way, because I have several mediaTypes (for example "application/vnd.springmvc3.sessionsinfo-v1+xml" and application/vnd.springmvc3.sessionsinfo-v2+xml"), and at the same time it seems that it's not possible to declare several contentTypes for the MappingJacksonJsonView/xmlMarshallingView.

Here a snippet of my dispatcher-servlet:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="order" value="1" />
  <property name="defaultViews">
    <list>
      <!-- JSON View -->
      <bean
    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <property name="contentType" value="application/*+json" />
      </bean>
      <!-- JAXB XML View -->
      <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
      <property name="contentType" value="application/*+xml"/>
        <constructor-arg>
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
               <property name="classesToBeBound">
                <list>
                   <value>org.springmvc3.models.SessionInfo</value>
                </list>
               </property>
            </bean>
        </constructor-arg>
      </bean>
     </list>
  </property>
  <property name="ignoreAcceptHeader" value="false" />
  <property name="FavorPathExtension" value="false" /> 

My Controller, which produces for example the mediaType "application/vnd.springmvc3.sessionsinfo-v1+xml":

@RequestMapping(method = RequestMethod.GET , produces = {"application/vnd.springmvc3.sessionsinfo-v1+json", "application/vnd.springmvc3.sessionsinfo-v1+xml"} )   
public ModelAndView showSessionsOverview(){

    SessionInfoService sessionInfoService = new SessionInfoService();
    SessionsInfo sessionsInfo = new SessionsInfo(sessionInfoService.getAllSessionInfoObjects());

    return new ModelAndView("all", "model",sessionsInfo);
}

Now I don't get any errors, but I neither get XML or JSON, even if I explicitly request it via the accept header. I only get html, which is like the fallback strategy in my configuration. But if I write "application/vnd.springmvc3.sessionsinfo-v1+xml" instead of "application/*+xml", it works great.

Any ideas how to make it work in a more generic way?

0

There are 0 best solutions below