Ignore a java bean field while converting to jSON
<p>Ignore a java bean field while converting to jSON</p>

<p>I am having a java bean and sending JSON as response , In that java bean I want to have
some transient fields , that should not come into JSON .</p>

<pre><code>@XmlRootElement(name = "sample")
class Sample{

private String field1;

@XmlTransient
private String transientField;

//Getter and setters

public String toJSON() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(this);
    return json;
}

}
</code></pre>

<p>When I am calling toJSON method I am still getting "transientField" in JSON.</p>

<p>And I have a get rest API that returns this Sample JSON as response.</p>

<pre><code>@GET
@Path("/somePath/")
@Produces({"application/json"})
Sample getSample();
</code></pre>

<p>In this response also I am getting that transient field .
Am I doing something wrong? Please help me to do this . </p>