简体   繁体   English

Java Jersey使用GET返回仅返回一些字段而不是全部字段的JSON

[英]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? naybody是否知道使用Jersey的GET方法返回仅返回实体的某些字段而不是全部的JSON的方法? 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? 有没有人知道使用Jersey的GET方法返回一个只返回实体的某些字段而不是全部的JSON的方法? 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'. 例如,在下面的课程中,我希望接收(使用POST)“name”和“机密”的值,返回时购买(使用GET)我只需要'name'值,而不是'机密'。

@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. 如果您使用的是JAXB方法,则可以使用@XmlTransient标记字段以省略它们。 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 . 如果您正在使用POJO映射或只想为某些请求排除字段,则应使用低级JSON API构造JSON

If you are using Jackson, you can use the annotation @JsonIgnore for methods 如果您使用的是Jackson,则可以使用@JsonIgnore注释方法

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. 标记注释类似于javax.xml.bind.annotation.XmlTransient,表示基于内省的序列化和反序列化功能将忽略带注释的方法。 That is, it should not be consider a "getter", "setter" or "creator". 也就是说,它不应该被认为是“吸气者”,“设定者”或“创造者”。

And @JsonIgnoreProperties for properties @JsonIgnoreProperties属性

Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization). 可用于抑制属性序列化(在序列化期间)或忽略处理读取的JSON属性的注释(在反序列化期间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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