gpt4 book ai didi

java - 如何在 RESTful WS 中隐藏带有条件的字段?

转载 作者:行者123 更新时间:2023-12-02 02:17:58 27 4
gpt4 key购买 nike

我有一个名为 Report 的类,需要使用 RESTful WS 进行共享。

  1. 一旦具备所有属性
  2. 曾经只是简化版

通常我会使用 @XmlTransient 之类的内容来隐藏字段,但这会阻止完整版本的工作。

是否有任何方法可以在输出之前设置条件或预过滤字段,以便它不会影响同一类的其他使用?

我的报告类如下所示:

public class Report {
private String reportId;
private String title;
private String content;
private Date created;
private Date modified;
...
}

完整报告的 RESTful 共享如下所示:

@GET
@Path("/{reportId}")
public Report getReport(@PathParam("reportId") String reportId) {
return Mock.getReport(reportId);
}

我需要的完整输出如下所示:

{
"reportId": "d83badf3",
"title": "The tales of lamaru",
"content": "When once the great Imgur started his journey...",
"created": 1519672434866,
"modified": 1519672434866
}

我需要的简短输出应如下所示:

{
"reportId": "d83badf3",
"title": "The tales of lamaru"
}

要实现这一目标需要什么?

最佳答案

为什么不使用继承

Parent

public class Report {
private String reportId;
private String title;
}

Child

public class FullReport extends Report{
private String content;
private Date createdp;
private Date modified;
}

当您需要完整报告时,请将返回类型设置为FullReport,否则Report

关于java - 如何在 RESTful WS 中隐藏带有条件的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com