gpt4 book ai didi

spring-mvc - Spring MVC 3.2 - XStreamAlias 被忽略

转载 作者:行者123 更新时间:2023-12-03 11:20:46 26 4
gpt4 key购买 nike

人.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 ...
}

Spring 配置:
<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 注释被忽略。是否需要进一步配置才能使其正常工作?

最佳答案

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

xstreamMarshaller.setAutodetectAnnotations(true);

See this project , 将以上行添加到 Application.java 中。

关于spring-mvc - Spring MVC 3.2 - XStreamAlias 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18702318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com