执行特定组的javax验证。

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

Executing specific group of javax validations

问题

在我的应用程序中,我有一个端点,该端点接收一个JSON对象,然后调用calculateSomething()来将数字作为HTTP响应返回。我正在使用javax.validation验证这些值。现在是否有可能让我指定如何验证Example类的对象,或者在这一个特定端点中要验证这个对象的哪些值(我有多个端点)?例如,在这种情况下,如果调用了这个端点,只有onetwothree将被验证,因为这些是calculateSomething()所需的唯一值。

类:

  1. @Entity
  2. @PrimaryKeyJoinColumn(name = "five")
  3. public class Example extends Foo {
  4. @ValidOne
  5. @Column
  6. private Integer one;
  7. @ValidTwo
  8. @Column
  9. private Integer two;
  10. @ValidThree
  11. @Column
  12. private Integer three;
  13. @ValidFour
  14. @Column
  15. private Integer four;
  16. @ValidFive
  17. @Column
  18. private Integer five;
  19. @Override
  20. public Integer calculateSomething() throws IllegalArgumentException{
  21. (one + two) * three
  22. }
  23. }

端点:

  1. @PostMapping("/calculateSomeNumber")
  2. public ResponseEntity calculateSomeNumber(@Valid @RequestBody Example example){
  3. return ResponseEntity.ok(example.calculateSomething());
  4. }
英文:

In my application I have an endpoint, that gets a JSON of this Object, which then calls calculateSomething() to return the number as a http response. I'm validating those values with javax.validation. Now is there a possible way for me, to specify how the object of the class Example is being validated or what values of this object will be validated in this one specific endpoint (I have multiple endpoints)? In this case for example, that if this endpoint gets called, that only one, two and three will be validated, because those are the only values needed for calculateSomething().

class:

  1. @Entity
  2. @PrimaryKeyJoinColumn(name = "five")
  3. public class Example extends Foo {
  4. @ValidOne
  5. @Column
  6. private Integer one;
  7. @ValidTwo
  8. @Column
  9. private Integer two;
  10. @ValidThree
  11. @Column
  12. private Integer three;
  13. @ValidFour
  14. @Column
  15. private Integer four;
  16. @ValidFive
  17. @Column
  18. private Integer five;
  19. @Override
  20. public Integer calculateSomething() throws IllegalArgumentException{
  21. (one + two) * three
  22. }
  23. }

endpoint:

  1. @PostMapping ("/calculateSomeNumber")
  2. public ResponseEntity calculateSomeNumber(@Valid @RequestBody Example example){
  3. return ResponseEntity.ok(example.calculateSomething());
  4. }

答案1

得分: 2

你可以声明接口,这些接口可以表示为组名称。然后,在定义验证约束时,将其应用于特定组。要仅使用特定的验证组进行验证,只需将其应用于相关的控制器方法。

  1. public interface ValidOne {
  2. }
  3. public interface ValidTwo {
  4. }
  5. public class SomeController {
  6. @PostMapping("/calculateSomeNumber")
  7. public ResponseEntity calculateSomeNumber(@Validated({ValidOne.class}) @RequestBody Example example){
  8. return ResponseEntity.ok(example.calculateSomething());
  9. }
  10. ...
  11. }
  12. @Entity
  13. @PrimaryKeyJoinColumn(name = "five")
  14. public class Example extends Foo {
  15. @Column
  16. @NotNull(groups = ValidOne.class)
  17. private Integer one;
  18. @Column
  19. @NotNull(groups = ValidTwo.class)
  20. private Integer two;
  21. ....
  22. }
英文:

You can declare interfaces which can signify as Group names. Then while defining a validation constraint apply it to specific group. To only validate using a specific validation group, simply apply it to relevant controller method

  1. public interface ValidOne {
  2. }
  3. public interface ValidTwo {
  4. }
  5. public class SomeController {
  6. @PostMapping ("/calculateSomeNumber")
  7. public ResponseEntity calculateSomeNumber(@Validated({ValidOne.class}) @RequestBody Example example){
  8. return ResponseEntity.ok(example.calculateSomething());
  9. }
  10. ...
  11. @Entity
  12. @PrimaryKeyJoinColumn(name = "five")
  13. public class Example extends Foo {
  14. @Column
  15. @NotNull(groups = ValidOne.class)
  16. private Integer one;
  17. @Column
  18. @NotNull(groups = ValidTwo.class)
  19. private Integer two;
  20. ....

huangapple
  • 本文由 发表于 2020年8月7日 22:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63304243.html
匿名

发表评论

匿名网友

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

确定