4

人.java:

@XStreamAlias("person")
public class Person {
    @XStreamAlias("id")
    private Long personProfileId;
    private String lastName;
    private String firstName;
    private String middleName;
    private String nameSuffix;
    private String namePrefix;

    // etc ...
}

弹簧配置:

<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="ignoreAcceptHeader" value="true" />
    <property name="useJaf" value="false" />
    <property name="defaultContentType" value="application/xml" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </map>
    </property>
</bean>

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="contentNegotiationManager" ref="contentNegotiationManager" />

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

            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
                </constructor-arg>
            </bean>
        </list>
    </property>
</bean>

<mvc:annotation-driven
    content-negotiation-manager="contentNegotiationManager" />

调用http://mycompany.com:8080/myapp/persons?format=xml返回:

<?xml version="1.0" encoding="UTF-8"?>
<list>
<com.example.myapp.model.Person>
    <personProfileId>1</personProfileId>
    <lastName>McCartney</lastName>
    <firstName>James</firstName>
    <middleName>Paul</middleName>
</com.example.myapp.model.Person>
<com.example.myapp.model.Person>
    <personProfileId>2</personProfileId>
    <lastName>Lennon</lastName>
    <firstName>John</firstName>
    <middleName>Winston</middleName>
</com.example.myapp.model.Person>
<com.example.myapp.model.Person>
    <personProfileId>3</personProfileId>
    <lastName>Starkey</lastName>
    <firstName>Richard</firstName>
</com.example.myapp.model.Person>
<com.example.myapp.model.Person>
    <personProfileId>4</personProfileId>
    <lastName>Harrison</lastName>
    <firstName>George</firstName>
</com.example.myapp.model.Person>
</list>

我希望它会返回:

<?xml version="1.0" encoding="UTF-8"?>
<list>
<Person>
    <id>1</id>
    <lastName>McCartney</lastName>
    <firstName>James</firstName>
    <middleName>Paul</middleName>
</Person>
<Person>
    <id>2</id>
    <lastName>Lennon</lastName>
    <firstName>John</firstName>
    <middleName>Winston</middleName>
</Person>
<Person>
    <id>3</id>
    <lastName>Starkey</lastName>
    <firstName>Richard</firstName>
</Person>
<Person>
    <id>4</id>
    <lastName>Harrison</lastName>
    <firstName>George</firstName>
</Person>
</list>

似乎 XStream 被正确调用以将对象编组为 XML,但是 @XStreamAlias 注释被忽略了。是否需要进一步配置才能使其正常工作?

4

3 回答 3

3

我试图弄清楚同样的事情(尽管使用 Spring Boot 项目)并找到了替代方案:autodetectAnnotations.

xstreamMarshaller.setAutodetectAnnotations(true);

看到这个项目,将上面的行添加到Application.java中。

于 2015-09-03T13:41:36.033 回答
1

Figured it out. The annotated classes need to be explicitly identified to the XStreamMarshaller.

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

        <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
            <constructor-arg>
                <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                    <property name="annotatedClasses">
                        <array>
                            <value>com.example.myapp.model.Person</value>
                        </array>
                    </property></bean>
            </constructor-arg>
        </bean>
    </list>
</property>

于 2013-09-09T18:52:25.613 回答
0

根据 XStream 文档https://x-stream.github.io/annotations-tutorial.html 你必须使用xstream.processAnnotations(MyPOJO.class);

于 2019-07-11T13:13:51.947 回答