0

背景

@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)。

4

1 回答 1

0

尝试将您的对象作为参数发送给 addPerson() 方法

public Person  addPerson(Person person){

    Person fme = new Person ();
        ...
}

并且不要忘记@XmlAccessorType(XmlAccessType.FIELD)之前的添加@XmlrootElement

于 2012-10-13T23:36:13.597 回答