【问题标题】:REST - How to display certain getters in javax.ws.rs.core.Response EntityREST - 如何在 javax.ws.rs.core.Response 实体中显示某些 getter
【发布时间】:2013-11-19 15:07:38
【问题描述】:

我想知道是否有任何方法可以注释 Entity getter,这样 ResponseBuilder 将只显示具有该特定注释的对象。

目前我开发了一个 REST api,我有一个包含 6 个字段的实体,我想在 GET 请求中只显示其中的 3 个。我想到的唯一解决方案是在实体类上创建一个包装器,它只包含我想在响应中显示的信息的获取器。但在此之前,我想知道是否有其他方法。

这就是我目前提供响应的方式。

Response.ok(device).build();

这就是我的实体的样子:

@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;
}

【问题讨论】:

  • 你不能使用注释 JsonIgnore (JSON) 和 XmlTransient (XML) 吗?
  • 是的,看来是对的,谢谢...

标签: java rest response


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 2012-03-14
    • 2011-08-21
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多