通过JAXB响应Spring xml

时间:2013-01-29 21:24:12

标签: spring jaxb marshalling

我使用的是Spring 3.1,我的应用程序已经设置为以json格式发送和接收数据。现在我需要提供一个请求API,它应该以xml格式返回相同的数据。 请帮我解决这个问题,或者说出我做错了什么。我尝试了JAXB,但是我收到“406 Not Acceptable”而不是xml。

我的要求API:

/** 
* Gets objects in json format
*/
@RequestMapping(value = "/objects/json", method = RequestMethod.GET)
@ResponseBody
public List<MyObjectTO> getAll() {
    List<MyObjectTO> objectsList = new ArrayList<MyObjectTO>();
    //forming objects
    return objectsList;
}
/** 
* Gets objects in xml format
*/
@RequestMapping(value = "/objects/xml", method = RequestMethod.GET,headers={"Accept=application/xml"})
@ResponseBody
public ResponseList getAll() {
    ResponseList objectsList = new ResponseList ();
    //the same formation
    return objectsList;
}

上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.kenshoo.urlbuilder"/>

    <mvc:annotation-driven/>
    <mvc:view-controller path="/mainpage" view-name="mainpage"/>
    <util:properties id="addProps" location="classpath:config/addProps.properties"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="alwaysUseFullPath" value="true"/>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="alwaysUseFullPath" value="true"/>
    </bean>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
                <entry key="pdf" value="application/pdf"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="cache" value="true"/>
                    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>

        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
            </list>
        </property>
    </bean>

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</beans>

我的更改:向AnnotationMethodHandlerAdapter添加了消息转换器并插入了JAXB marshaller。但在此之后得到了回应“406 Not Acceptable”:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="alwaysUseFullPath" value="true"/>
    <property name="messageConverters">
       <list>
            <ref bean="marshallingConverter" />
       </list>
   </property>
</bean>

<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
     <property name="marshaller" ref="jaxbMarshaller" />
      <property name="unmarshaller" ref="jaxbMarshaller" />
      <property name="supportedMediaTypes" value="application/xml"/>
</bean>

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
          <list>
            <value>com.ResponseList</value>
          </list>
    </property>
</bean>

感谢您的帮助,谢谢。

UPDATE ResponseList结构:

public class ResponseList {

    private List<FirstLevel> firstLevelObjects;

    public List<FirstLevel> getFirstLevelObjects() {
        return firstLevelObjects;
    }

    public void setFirstLevelObjects(List<FirstLevel> firstLevelObjects) {
        this.firstLevelObjects= firstLevelObjects;
    }

}

FirstLevel结构:

public class FirstLevel {
    List<SecondLevel> secondLevelObjects; 
    boolean isConditional;
    String beforeStart;
    ConditionType type;//enum object
    //...getters and setters
}

1 个答案:

答案 0 :(得分:2)

  

我对Castor失去了希望,所以再次尝试使用JAXB。改了帖子   有关JAXB的详细信息。同样的问题 - 我什么时候不能接受   试图请求xml。你能救我吗?

您需要为JAXB做的只是为@XmlRootElement课程添加ResponseList注释。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ResponseList {

    private List<FirstLevel> firstLevelObjects;

    public List<FirstLevel> getFirstLevelObjects() {
        return firstLevelObjects;
    }

    public void setFirstLevelObjects(List<FirstLevel> firstLevelObjects) {
        this.firstLevelObjects= firstLevelObjects;
    }

}