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

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

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

创建优惠券:

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

更新优惠券:

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

以及CouponDto:

  1. public class CouponCodeDto extends BaseDto<Integer> {
  2. @NotBlank
  3. private String code;
  4. private Integer availableCount;
  5. @NotNull
  6. private LocalDate startDate;
  7. // ...
  8. }
英文:

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:

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

For update a coupon:

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

And CouponDto:

  1. public class CouponCodeDto extends BaseDto&lt;Integer&gt; {
  2. @NotBlank
  3. private String code;
  4. private Integer availableCount;
  5. @NotNull
  6. private LocalDate startDate;
  7. ...
  8. }

答案1

得分: 4

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

  1. public class Views {
  2. interface Update {}
  3. interface Create extends Update {}
  4. }
  1. public class CouponCodeDto extends BaseDto<Integer> {
  2. @NotBlank
  3. @JsonView(Views.Create.class)
  4. private String code;
  5. @JsonView(Views.Update.class)
  6. private Integer availableCount;
  7. @NotNull
  8. @JsonView(Views.Update.class)
  9. private LocalDate startDate;
  10. // ...
  11. }

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

  1. @PutMapping("/update/{id}")
  2. public ResponseEntity<?> updateCoupon(@PathVariable Long id,
  3. @JsonView(Views.Update.class) @RequestBody CouponCodeDTO couponCodeDTO) {
  1. @PostMapping("/create")
  2. public ResponseEntity<?> createCoupon(
  3. @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.

  1. public class Views {
  2. interface Update {}
  3. interface Create extends Update {}
  4. }
  5. public class CouponCodeDto extends BaseDto&lt;Integer&gt; {
  6. @NotBlank
  7. @JsonView(Views.Create.class)
  8. private String code;
  9. @JsonView(Views.Update.class)
  10. private Integer availableCount;
  11. @NotNull
  12. @JsonView(Views.Update.class)
  13. private LocalDate startDate;
  14. ...
  15. }

And use on request body of the endpoint

  1. @PutMapping(&quot;/update/{id}&quot;)
  2. public ResponseEntity&lt;?&gt; updateCoupon(@PathVariable Long id,
  3. @JsonView(Views.Update.class) @RequestBody CouponCodeDTO couponCodeDTO) {
  4. @PostMapping(&quot;/create&quot;)
  5. public ResponseEntity&lt;?&gt; createCoupon(
  6. @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:

确定