0

The non default constructor is never called in my Person class when I attempt to consume rest service which takes Person as a parameter.

Here is my Person Class:

@XmlRootElement(name = "person")
public class Person {

protected String name;

public Person(String name) {
    this.name=name;
}

// default constructor
public Person(){this.name="";}

@XmlElement(required=true, name="name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
};

}

Here is my web method:

@Path("person/")
public class PostResource {

@Path("create")
@POST
@Produces("text/plain")
@Consumes("application/json")
public String createPerson(Person person) {
    return person.getName();
}
}

Here is the body of my rest call:

{

"person": { "name":"some name"}

}

The name of the person object passed createPerson is always blank (or whatever I specify in the default constructor)

Any clue why??? Also the method does return successfully so it is responding right just not accepting the object properly

MobileMon
  • 8,341
  • 5
  • 56
  • 75
  • What you have looks like it should work for this JSON: `{ "name":"some name"}`. Do you see how that's different from the JSON you're POSTing? – Matt Ball Apr 08 '13 at 21:22
  • I see how its different yes, although i just tried it with { "name":"some name"} and still got the same result – MobileMon Apr 08 '13 at 21:27
  • [It's supposed to be using the default constructor.](http://stackoverflow.com/questions/4387296/jaxb-and-constructors) The question is, why isn't it calling the setter? – Thomas Apr 08 '13 at 21:58
  • 1
    Did you set `com.sun.jersey.api.json.POJOMappingFeature` ? – Willy Apr 09 '13 at 04:00
  • Did not set POJO mapping, nor did I have a JAXBContextResolver. Works now! – MobileMon Apr 10 '13 at 15:29

0 Answers0