背景
@XmlRootElement
public class Person {
private String firstName;
private String lastName;
...//accessors
}
@Path("mypath")
public class PersonResource{
@POST
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response addPerson(JAXBElement<Person> jaxbPerson) {
Person person = jaxbPerson.getValue();
...//logic etc.
}
}PersonResource. addPerson将接受{"firstName":"Alfred","lastName":"Bell"},但不接受{"person":{"firstName":"Alfred","lastName":"Bell"}}。
正因为如此,我有以下问题。
问题:
给定的
@XmlRootElement
public class car {
private String maker;
private String model;
private AirBag airbag;
private List<Tire> tires;
@XmlElementWrapper(name = "tires")
@XmlElement(name = "tire")
public Set<Tire> get Tires() {
return this.tires;
}
...// more accessors
}
@Path("add-car")
public class CarResource{
@POST
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response addPerson(JAXBElement<Car> jaxbCar) {
Car car = jaxbCar.getValue();
...//logic etc.
}
}如何格式化JSON,以便JAXBElement<Car> jaxbCar能够识别它?汽车必须有四个轮胎和一个安全气囊。
详细信息:
我正在使用Jersey (Java REST-API)。
发布于 2012-10-14 07:36:14
尝试将对象作为参数发送到addPerson()方法
public Person addPerson(Person person){
Person fme = new Person ();
...
}别忘了在@XmlrootElement之前添加@XmlAccessorType(XmlAccessType.FIELD)
https://stackoverflow.com/questions/12876245
复制相似问题