【发布时间】:2019-02-23 09:10:42
【问题描述】:
有spring-boot应用(web+jpa) 所以,我有控制器:
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping(value = "/customers", method = RequestMethod.GET)
public @ResponseBody List<Customer> findAllCustomers() {
return customerService.findAllCustomers();
}
@RequestMapping(value = "/customers", method = RequestMethod.POST)
public void addCustomer(@RequestBody Customer customer) {
customerService.addCustomer(customer);
}
型号:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name="customer")
@XmlRootElement(name="customer")
public class Customer{
@Id
private String id;
private String name;
public Customer(String id, String name) {
this.id = id;
this.name = name;
}
public Customer() {
}
public String getId() {
return id;
}
@XmlElement
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
}
以及绑定jpa和rest的服务层。
当我请求时:
<customer>
<id>first</id>
<name>first name of metric</name>
</customer>
没关系,客户已添加到数据库中,但是,当我尝试获取所有客户时,响应为 json 格式,但我期望的是 xml。如何解决它的问题?
【问题讨论】:
-
您不能公开 XML 列表。您还需要一个 XML 包装器。要返回的 XML 必须是有效的 XML。
标签: xml spring-boot