简体   繁体   English

REST - 如何在javax.ws.rs.core.Response实体中显示某些getter

[英]REST - How to display certain getters in javax.ws.rs.core.Response Entity

I was wondering if there are any ways to annotate Entity getters in such way that the ResponseBuilder will display only the ones which have that specific annotation. 我想知道是否有任何方法来注释Entity getter,使ResponseBuilder只显示具有该特定注释的那些。

Currently I develop a REST api, and I have an entity with 6 fields, and I want to display only 3 of them on a GET request. 目前我开发了一个REST api,我有一个包含6个字段的实体,我想在GET请求中只显示其中的3个。 The only solution which came into my mind is to create a wrapper over the entity class, which has getters only for the information I would like to display in the response. 我想到的唯一解决方案是在实体类上创建一个包装器,它只包含我希望在响应中显示的信息的getter。 But before doing that, I want to know if there is another way. 但在此之前,我想知道是否还有其他方法。

This is how I currently serve the response. 这就是我目前为响应服务的方式。

Response.ok(device).build();

And this is how my entity looks like : 这就是我的实体的样子:

@Entity
@XmlRootElement
@IdClass(DevicePK.class)
public class Device implements Serializable {

private static long serialVersionUID = 1l;

@Id
private String deviceId;

private String ssoId;

@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;

@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdateAt;

@Id
@Enumerated(EnumType.STRING)
private Platform platform;

@Temporal(TemporalType.TIMESTAMP)
private Date associationDate;

/**
 * @return the deviceId
 */
public String getDeviceId() {
    return deviceId;
}

/**
 * @param deviceId the deviceId to set
 */
public void setDeviceId(String deviceId) {
    this.deviceId = deviceId;
}

/**
 * @return the ssoId
 */
public String getSsoId() {
    return ssoId;
}

/**
 * @param ssoId the ssoId to set
 */
public void setSsoId(String ssoId) {
    this.ssoId = ssoId;
}

/**
 * @return the createdAt
 */

public Date getCreatedAtAsDate() {
    return createdAt;
}

/**
 * @param createdAt the createdAt to set
 */
public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
}

/**
 * @return the lastUpdateAt
 */
public String getLastUpdateAt() {
    return lastUpdateAt;
}

/**
 * @param lastUpdateAt the lastUpdateAt to set
 */
public void setLastUpdateAt(Date lastUpdateAt) {
    this.lastUpdateAt = lastUpdateAt;
}

/**
 * @return the platform
 */
public Platform getPlatform() {
    return platform;
}

/**
 * @param platform the platform to set
 */
public void setPlatform(Platform platform) {
    this.platform = platform;
}

/**
 * @return the associationDate
 */
public String getAssociationDate() {
    return associationDate;
}

/**
 * @param associationDate the associationDate to set
 */
public void setAssociationDate(Date associationDate) {
    this.associationDate = associationDate;
}

您应该使用@XmlTransient注释要从响应中排除的字段。

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

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