JSON response: Spring with JAXB

我是研究僧i 提交于 2019-12-11 10:18:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!