[英]Ignore specific properties conditionally DTO
I have a specific requirement, I have a single POJO like EmployeeDTO.java 我有一个特定的要求,我有一个像EmployeeDTO.java的POJO
with below Fields; 以下字段;
int employeeId;
String Name;
double salary;
String createdBy;
Date createdTimestamp;
String updatedBy;
Date updatedTimestamp;
Now my reqirement is while I will get a Create API request as a JOSN I shouldn't get any fields like employeeId, createdBy, createdTimestamp, updatedBy, updatedTimestamp. 现在我需要的是,当我将收到一个JOSN的Create API请求时,我不应该获得任何字段,例如employeeId,createdBy,createdTimestamp,updatedBy,updatedTimestamp。
But in a GET API response, I should send all these fields. 但是在GET API响应中,我应该发送所有这些字段。
I can't use jsonIgnore here as for a field like employeeId I am not expcting it in the request, but I want to send it in the response. 我不能在这里使用jsonIgnore,因为对于诸如employeeId之类的字段,我不在请求中不使用它,但我想在响应中发送它。
In a PATCH/PUT api call I am expecting employeeId from UI in the request but not below 4 fields createdBy, createdTimestamp, updatedBy, updatedTimestamp. 在PATCH / PUT api调用中,我期望请求中的UI中有employeeId,但不低于4个字段createdBy,createdTimestamp,updatedBy,updatedTimestamp。
So a simple solutions can be I can create separate DTOs, like EmployeeDTO, EmployeeCreateRequestDTO, EmployeeUpdateRequestDTO. 因此,一个简单的解决方案就是我可以创建单独的DTO,例如EmployeeDTO,EmployeeCreateRequestDTO,EmployeeUpdateRequestDTO。 But it will leads to duplication of codes. 但这将导致代码重复。
Is there any better way or json feature/annotation is available to achieve this ? 有没有更好的方法或json功能/注释可用于实现此目的?
The reason I want this, In the Swagger Model Schema it is displaying all the fields available in EmplyeeDTO, I want to be specific here for a Create request A user should not see any of the the fields which are getting set in back end like id, createdBy etc. 我想要这个的原因,在Swagger模型架构中,它显示了EmplyeeDTO中所有可用的字段,我想在这里针对创建请求进行具体说明用户不应该看到在id中后端设置的任何字段,createdBy等。
Using specific classes that represent clearly the data/behavior of the business use case is better (in terms of readability, maintainability) than mixing things that should not be. 使用明确表示业务用例的数据/行为的特定类(在可读性,可维护性方面)比混合不应该使用的类更好。
So a simple solutions can be I can create separate DTOs, like EmployeeDTO, EmployeeCreateRequestDTO, EmployeeUpdateRequestDTO. 因此,一个简单的解决方案就是我可以创建单独的DTO,例如EmployeeDTO,EmployeeCreateRequestDTO,EmployeeUpdateRequestDTO。 But it will leads to duplication of codes. 但这将导致代码重复。
It will not create necessarily code duplication if all common fields to EmployeeCreateRequestDTO
and EmployeeUpdateRequestDTO
are present only in EmployeeDTO
. 如果EmployeeCreateRequestDTO
和EmployeeUpdateRequestDTO
所有公共字段仅存在于EmployeeDTO
则它不会创建必要的代码重复。
To avoid the duplication, you have two ways. 为避免重复,您有两种方法。
1) Inheritancy 1)继承
EmployeeCreateRequestDTO
and EmployeeUpdateRequestDTO
are subclasses of EmployeeDTO
. EmployeeCreateRequestDTO
和EmployeeUpdateRequestDTO
是子类EmployeeDTO
。
Advantage : subclasses have a direct access to the public common getters/setters of the EmployeeDTO
parent 优势:子类可以直接访问EmployeeDTO
父级的公共通用获取者/设置者
2) Composition 2)组成
Each one of these specific classes wraps a EmployeeDTO
instance and define their own fields. 这些特定类中的每一个都包装EmployeeDTO
实例并定义自己的字段。
Advantage : subclasses have more flexibility to define which should be accessible from the EmployeeDTO
field. 优点:子类具有更大的灵活性来定义应从EmployeeDTO
字段访问的子类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.