Ignore a property in Dto from RequestBody base on endpoint (use one Dto for two purpose)

huangapple go评论86阅读模式
英文:

Ignore a property in Dto from RequestBody base on endpoint (use one Dto for two purpose)

问题

在数据库中我有一个优惠券表,所以在应用程序中有CouponEntity和CouponDto。我的问题是关于如何处理使用一个Dto的两个端点。例如,我有创建(create)和更新(update)端点,它们都使用CouponDto。有一个业务规则,我不能更新优惠券实体的“code”属性。如果客户在请求中传递了这个属性,如何忽略它?

如果我使用@JsonIgnore,这个属性将在我需要在创建API中获取它的两个端点中被忽略。我使用了groups进行验证,并将验证从彼此分开,但有时无法设置@JsonIgnore

创建优惠券:

@PostMapping("/create")
public ResponseEntity<?> createCoupon(@RequestBody CouponCodeDTO couponCodeDTO) {
    return ResponseEntity.ok(couponService.create(couponCodeDTO));
}

更新优惠券:

@PutMapping("/update/{id}")
public ResponseEntity<?> updateCoupon(
    @PathVariable Long id, @RequestBody CouponCodeDTO couponCodeDTO
) {
    return ResponseEntity.ok(couponService.update(id, couponCodeDTO));
}

以及CouponDto:

public class CouponCodeDto extends BaseDto<Integer> {
    
    @NotBlank
    private String code;
    
    private Integer availableCount;
    
    @NotNull
    private LocalDate startDate;
    
    // ...
}
英文:

I have a Coupon Table in the database so there are CouponEntity and CouponDto in the application. My question is about handling two endpoints with one Dto. for example, I have create and update endpoints, both of them use CouponDto. There is a business role that I can't update code property of the coupon entity. How to ignore it if the client passes this property in the request?

If I use @JsonIgnore, the property will be ignored in both the endpoints that I need to get it in create API. I used groups for validations and separate validations from each other, but @JsonIgnore can not be set sometimes.

For create a coupon:

@PostMapping(&quot;/create&quot;)
public ResponseEntity&lt;?&gt; createCoupon(@RequestBody CouponCodeDTO couponCodeDTO) {
    return ResponseEntity.ok(couponService.create(couponCodeDTO));
}

For update a coupon:

@PutMapping(&quot;/update/{id}&quot;)
public ResponseEntity&lt;?&gt; updateCoupon(
    @PathVariable Long id, @RequestBody CouponCodeDTO couponCodeDTO
) {
    return ResponseEntity.ok(couponService.update(id, couponCodeDTO));
}

And CouponDto:

public class CouponCodeDto extends BaseDto&lt;Integer&gt; {

    @NotBlank
    private String code;

    private Integer availableCount;

    @NotNull
    private LocalDate startDate;
    
   ...
}

答案1

得分: 4

你可以使用 @JsonView 来指定每个视图中要序列化/反序列化的字段,并在端点上指定视图。

public class Views {
    interface Update {}
    interface Create extends Update {}
}
public class CouponCodeDto extends BaseDto<Integer> {

    @NotBlank
    @JsonView(Views.Create.class)
    private String code;

    @JsonView(Views.Update.class)
    private Integer availableCount;

    @NotNull
    @JsonView(Views.Update.class)
    private LocalDate startDate;
    // ...
}

然后在端点的请求体中使用:

@PutMapping("/update/{id}")
public ResponseEntity<?> updateCoupon(@PathVariable Long id,
                   @JsonView(Views.Update.class) @RequestBody CouponCodeDTO couponCodeDTO) {
@PostMapping("/create")
public ResponseEntity<?> createCoupon(
                   @JsonView(Views.Create.class) @RequestBody CouponCodeDTO couponCodeDTO) {

你可以在以下链接找到详细信息:

英文:

You can use @JsonView to specify the field you want to serialize/deserialize per view and specify view on the endpoint.

public class Views {
	interface Update {}
	interface Create extends Update {}
}

public class CouponCodeDto extends BaseDto&lt;Integer&gt; {

    @NotBlank
    @JsonView(Views.Create.class)
    private String code;

    @JsonView(Views.Update.class)
    private Integer availableCount;

    @NotNull
    @JsonView(Views.Update.class)
    private LocalDate startDate;
    
   ...
}

And use on request body of the endpoint

@PutMapping(&quot;/update/{id}&quot;)
public ResponseEntity&lt;?&gt; updateCoupon(@PathVariable Long id, 
                   @JsonView(Views.Update.class) @RequestBody CouponCodeDTO couponCodeDTO) {

@PostMapping(&quot;/create&quot;)
public ResponseEntity&lt;?&gt; createCoupon(
                   @JsonView(Views.Create.class) @RequestBody CouponCodeDTO couponCodeDTO) {

Here you find details

https://www.baeldung.com/jackson-json-view-annotation

https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

huangapple
  • 本文由 发表于 2020年10月25日 14:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64520825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定