简体   繁体   English

有条件地忽略DTO的特定属性

[英]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 . 如果EmployeeCreateRequestDTOEmployeeUpdateRequestDTO所有公共字段仅存在于EmployeeDTO则它不会创建必要的代码重复。

To avoid the duplication, you have two ways. 为避免重复,您有两种方法。

1) Inheritancy 1)继承
EmployeeCreateRequestDTO and EmployeeUpdateRequestDTO are subclasses of EmployeeDTO . EmployeeCreateRequestDTOEmployeeUpdateRequestDTO是子类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.

相关问题 如何使用Jackson AnnotationIntrospector有条件地忽略属性 - How to conditionally ignore properties with a Jackson AnnotationIntrospector 忽略标有特定注释的属性 - Ignore properties marked with specific annotation MapStruct 忽略特定方法的所有未映射属性 - MapStruct ignore all unmapped properties for specific method 如何使用Spring MVC忽略DTO对象中缺少的属性? - How can I ignore missing properties in my DTO objects with Spring MVC? 用Jackson进行反序列化(对未知属性失败)不会忽略鉴别属性(使用SwaggerCodegen创建的DTO) - Deserializing with Jackson (fail on unknown properties) does not ignore discriminator property (DTO's created with SwaggerCodegen) 以编程方式忽略(省略)REST 服务的 JSON 响应中的特定字段,而不更改 DTO - Programmatically ignore (omit) specific fields in JSON response of REST service WITHOUT altering the DTO object class Java Jackson序列化会忽略带有注释的特定嵌套属性 - Java Jackson Serialization ignore specific nested properties with Annotations Mapstruct :有条件地映射字段或忽略 - Mapstruct : map field conditionally or ignore 如何在dto属性上使用SafeHtml注释? - How to use SafeHtml annotation on dto properties? 使用BeanUtils忽略子类中的属性 - Ignore properties in subclass with BeanUtils
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM