我访问了所有论坛帖子,但我仍然无法解决我的问题。我有一个应该返回XML和JSON结果的应用程序。但只有JSON工作,XML请求返回406错误。我正在使用Spring 3.2。除了http http状态之外,日志中没有错误。我尝试了以下配置:
配置1 :
<beans...>
<context:component-scan base-package="com.sample" />
<!-- First version -->
<bean id="xmlConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.sample.Class</value>
</list>
</property>
</bean>
</constructor-arg>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application" />
<constructor-arg index="1" value="xml" />
<constructor-arg index="2" value="UTF-8" />
</bean>
</list>
</property>
</bean>
<bean id="jaxbAnnotationInspector" class="org.codehaus.jackson.xc.JaxbAnnotationIntrospector" />
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
<property name="annotationIntrospector" ref="jaxbAnnotationInspector" />
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application" />
<constructor-arg index="1" value="json" />
<constructor-arg index="2" value="UTF-8" />
</bean>
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="xmlConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
</beans>
配置2 :
<beans ...>
<context:component-scan base-package="com.sample" />
<mvc:annotation-driven />
<!-- Second version -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="defaultContentType" value="application/xml" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.sample.Class</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="favorPathExtension" value="true" />
<property name="ignoreAcceptHeader" value="false" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
</beans>
My Maven依赖项:
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<scope>compile</scope>
</dependency>
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.10</version>
</dependency>
<!-- XML Mapper -->
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-oxm-tiger</artifactId>
<version>1.5.10</version>
</dependency>
我的方法:
@RequestMapping(value = "/repositories", method = RequestMethod.GET)
@ResponseBody
public List<Repository> getRepositories(HttpServletRequest req, HttpServletResponse resp) throws CaraException {
List<Repository> repositories = repositoryFactory.getAvailableRepositories();
return repositories;
}
我的班级用JAXB注释:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Repository
[编辑]
我缩小了这个问题。看起来它与我返回的java.util.List有关。如果我尝试返回使用JAXB注释的任何其他对象 - 它可以工作。那么问题是如何使List工作?
提前感谢您提供任何帮助。
马里乌什
答案 0 :(得分:1)
我找到了解决方案。而不是List我必须创建一个列表包装器并返回该包装器。显然JAXB无法处理列表。
@XmlRootElement(name = "repositories")
public class RepositoryListWrapper {
private List<Repository> list;
protected RepositoryListWrapper() {
} // Keep JAXB happy
public RepositoryListWrapper(List<Repository> list) {
this.list = list;
}
@XmlElement(name = "repository")
public List<Repository> getRepositories() {
return list;
}
}