1

我即将对我的 REST Web 服务进行版本控制,它基于 ContentNegotiatingViewResolver 以允许我返回 XML、JSON 以及 HTML 视图。

问题是,我既不能在 MappingJacksonJsonView 的 contentType 中使用通配符,也不能在 xmlMarshallingView 的 contentType 中使用通配符,它​​们都被 ContentNegotiatingViewResolver 引用。但我需要以通用方式定义这些 contentTypes,因为我有几个 mediaTypes(例如“application/vnd.springmvc3.sessionsinfo-v1+xml”和 application/vnd.springmvc3.sessionsinfo-v2+xml“),并且在同时似乎不可能为 MappingJacksonJsonView/xmlMarshallingView 声明多个 contentType。

这是我的调度程序 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" /> 

我的控制器,例如产生 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);
}

现在我没有收到任何错误,但我也没有收到 XML 或 JSON,即使我通过接受标头明确请求它。我只得到 html,这就像我的配置中的后备策略。但是如果我写“application/vnd.springmvc3.sessionsinfo-v1+xml”而不是“application/*+xml”,它会很好用。

任何想法如何使它以更通用的方式工作?

4

0 回答 0