背景
@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
识别它?汽车必须有四个轮胎和一个安全气囊。
细节:
我正在使用泽西岛(Java REST-API)。