简体   繁体   中英

Java Jersey use GET to return JSON that returns only some fields instead of all

Does naybody knows a way to use Jersey's GET method to return a JSON that returns only some fields of an entity instead of all? Does anybody know a way to use Jersey's GET method to return a JSON that returns only some fields of an entity instead of all? Eg in the following class I want to receive (with POST) values for 'name' and for 'confidential', buy while returning (with GET) I only need 'name' value, not 'confidential'.

@Entity
@Table(name = "a")
@XmlRootElement
@JsonIgnoreProperties({"confifentialInfo"})
public class A extends B implements Serializable {
    private String name;
    @Basic(optional = false)
    private String confifentialInfo;
    // more fields, getters and setters
}

If you are using the JAXB approach, you can mark fields with @XmlTransient to omit them. If you are using POJO mapping or want to exclude fields only for some requests, you should construct the JSON with the low level JSON API .

If you are using Jackson, you can use the annotation @JsonIgnore for methods

Marker annotation similar to javax.xml.bind.annotation.XmlTransient that indicates that the annotated method is to be ignored by introspection-based serialization and deserialization functionality. That is, it should not be consider a "getter", "setter" or "creator".

And @JsonIgnoreProperties for properties

Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM