2011-06-10 48 views
1

我知道使用mvc:注解驱动会给我免费的。但我想对它有更多的控制。如何配置spring mvc 3使用jaxb2 for xml MarshallingView里面的ContentNegotiatingViewResolver?

我的配置是这样的:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <property name="order" value="1" /> 
     <property name="mediaTypes"> 
      <map> 
       <entry key="json" value="application/json" /> 
       <entry key="xml" value="text/xml"/> 
      </map> 
     </property> 
     <property name="defaultViews"> 
      <list> 
       <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"> 
        <property name="prefixJson" value="false" /> 
        <property name="objectMapper" ref="jacksonObjectMapper" /> 
       </bean> 
       <bean class="org.springframework.web.servlet.view.xml.MarshallingView" > 
        <property name="marshaller"> 
          <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
          <property name="contextPath" value="com.appservices.dtos"/> 
         </bean> 
        </property> 
       </bean> 
      </list> 
     </property> 
    </bean> 

春不喜欢的contextPath特性,如何分辨它扫描了一些包装,拿出所有JAXB豆?谢谢。

+0

在应用程序上下文初始化过程中,您是否遇到任何异常?你使用什么春季版本?还要记住contextPath中指定的包是用:分开的,并且没有看到子包,即dto类必须在同一个确切的包中。 – ilcavero 2011-06-13 18:14:26

回答

2

你试过用classesToBeBound属性吗?在任何情况下,你的问题无关与Spring MVC的或contentnegotiatingViewResolver,与Jaxb2Marshaller

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
    <property name="classesToBeBound"> 
     <list> 
      <value>org.springframework.oxm.jaxb.Flight</value> 
      <value>org.springframework.oxm.jaxb.Flights</value>     
     </list> 
    </property> 
    <property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/> 
</bean> 
+1

我知道这会起作用。但我不想列出所有的jaxb类。在这种方法中,您将不得不创建“​​集合”类,即您需要为“Flight”类创建“Flights”类。 – Bobo 2011-06-13 18:00:38

2

在Spring 3.1中,新属性“packagesToScan”被添加到Jaxb2Marshaller中,它执行您期望的操作。然而,仍然有一个错误禁止它正常工作,但你可以期待下一个版本将有这种修复(我希望)。

+0

似乎现在工作(4.2) – z0r 2016-05-23 06:23:02