0

BACKGROUNG

@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 will accept {"firstName":"Alfred","lastName":"Bell"} but not {"person":{"firstName":"Alfred","lastName":"Bell"}}.

Because of that I have the following problem.

PROBLEM:

GIVEN

@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.
   }     
}

How do I format the JSON so that JAXBElement<Car> jaxbCar recognizes it? The car must have four tires and an airbag.

DETAILS:

I am using Jersey (Java REST-API).

kasavbere
  • 5,873
  • 14
  • 49
  • 72

1 Answers1

0

Try to send your object as argument to the addPerson() method

public Person  addPerson(Person person){

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

And don't forget the add @XmlAccessorType(XmlAccessType.FIELD) right before @XmlrootElement

abdellah7000
  • 11
  • 1
  • 2
  • What if the client is a website where will they get the `Person` object from? – kasavbere Oct 14 '12 at 01:03
  • Normaly when you want to post data, you need a form on your website pages that allow sending the person object in JSON file,all what you have to do is annotating you `addPerson()` method with `@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})` – abdellah7000 Oct 16 '12 at 11:28