java使用REST从Spring MVC控制器返回XML
我无法更改Spring MVC控制器返回的XML中标记的名称,SRSpring将XML标记作为Java对象名返回,而不是按预期返回。我已经使用@XmlElement根据需要设置了标记名,但不起作用
我曾尝试在属性处添加XmlElement而不是getter,但没有成功,我已经寻找了很多解决方案,但都没有成功,我已经添加了所有必要的JAXB注释,以使其发挥作用,请帮助我,因为我无法在这方面取得进一步进展
控制器:
@RequestMapping(value = "/customerAttributes/{number}", method =
RequestMethod.GET, produces = "application/xml")
@ResponseBody
public Customer getCustInfo(@PathVariable String number) {
Address add = new Address();
Customer cust = new Customer();
add.setHouse(number);
add.setStreet("Street");
cust.setName("Ankush");
cust.setAdd(add);
return cust;
}
顾客:
package com.fti.di.customer.domain;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"Name","add"})
@XmlRootElement(name = "DI_Customer")
public class Customer {
private String name;
private Address add;
@XmlElement(name = "DI_Name", required = true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "DI_Add", required = true)
public Address getAdd() {
return add;
}
public void setAdd(Address add) {
this.add = add;
}
}
地址:
package com.fti.di.customer.domain;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(name = "",propOrder = {"Street","House"})
public class Address {
private String street;
private String house;
@XmlElement(name = "DI_Street")
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@XmlElement(name = "DI_House")
public String getHouse() {
return house;
}
public void setHouse(String house) {
this.house = house;
}
}
结果:
<Customer>
<add>
<street>Street</street>
<house>3012</house>
</add>
<name>Ankush</name>
</Customer>
预计:
<DI_Customer>
<DI_Add>
<DI_Street>Street</DI_Street>
<DI_House>3012</DI_House>
</DI_Add>
<DI_Name>Ankush</DI_Name>
</DI_Customer>
# 1 楼答案