问题
Tech Stack: Java 1.6, JAXB, Spring 3, JAX-RS (RESTEasy), XSD
Hello,
I am using Spring with JAX-RS to create RestFul Webservice.
Everything is working fine except that the generated responses contain the setters info e.g.
{
...
"setName": true,
"setId": true,
"setAddress": true,
"setAge": true,
}
I don't know what might be causing this? How can I turn this off?
Adi
UPDATE 1:
The PersonRequest class is generated by the JAXB and contains all the javax.xml.bind.annotation.* annotations.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "personResponse " })
@XmlRootElement(name = "PersonResponse ")
public class PersonResponse {
@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Id", required = true)
protected String id;
// and the setters and getters
}
and the Resource looks like this:
@Component
@Path("/person")
public class PersonImpl implements Person {
@Override
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "application/x-amf" })
@Path("v1")
public PersonResponse getPerson() {
....
....
}
}
** UPDATE 2 ** This happens only when Content-Type is json, in case of Content Type as 'xml', the setters are not returned. If that helps.
回答1:
I suspect some other part of the stack is weaving in extra fields to your domain model (I.e some ORM libraries do this). To confirm you could use the java.lang.reflect
APIs to see what fields your class has after it had been loaded by the ClassLoader
.
回答2:
Problem was in the xjb file, for details look in the related question here.
来源:https://stackoverflow.com/questions/12620450/json-response-spring-with-jaxb