英文:
Executing specific group of javax validations
问题
在我的应用程序中,我有一个端点,该端点接收一个JSON对象,然后调用calculateSomething()
来将数字作为HTTP响应返回。我正在使用javax.validation
验证这些值。现在是否有可能让我指定如何验证Example
类的对象,或者在这一个特定端点中要验证这个对象的哪些值(我有多个端点)?例如,在这种情况下,如果调用了这个端点,只有one
、two
和three
将被验证,因为这些是calculateSomething()
所需的唯一值。
类:
@Entity
@PrimaryKeyJoinColumn(name = "five")
public class Example extends Foo {
@ValidOne
@Column
private Integer one;
@ValidTwo
@Column
private Integer two;
@ValidThree
@Column
private Integer three;
@ValidFour
@Column
private Integer four;
@ValidFive
@Column
private Integer five;
@Override
public Integer calculateSomething() throws IllegalArgumentException{
(one + two) * three
}
}
端点:
@PostMapping("/calculateSomeNumber")
public ResponseEntity calculateSomeNumber(@Valid @RequestBody Example example){
return ResponseEntity.ok(example.calculateSomething());
}
英文:
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:
@Entity
@PrimaryKeyJoinColumn(name = "five")
public class Example extends Foo {
@ValidOne
@Column
private Integer one;
@ValidTwo
@Column
private Integer two;
@ValidThree
@Column
private Integer three;
@ValidFour
@Column
private Integer four;
@ValidFive
@Column
private Integer five;
@Override
public Integer calculateSomething() throws IllegalArgumentException{
(one + two) * three
}
}
endpoint:
@PostMapping ("/calculateSomeNumber")
public ResponseEntity calculateSomeNumber(@Valid @RequestBody Example example){
return ResponseEntity.ok(example.calculateSomething());
}
答案1
得分: 2
你可以声明接口,这些接口可以表示为组名称。然后,在定义验证约束时,将其应用于特定组。要仅使用特定的验证组进行验证,只需将其应用于相关的控制器方法。
public interface ValidOne {
}
public interface ValidTwo {
}
public class SomeController {
@PostMapping("/calculateSomeNumber")
public ResponseEntity calculateSomeNumber(@Validated({ValidOne.class}) @RequestBody Example example){
return ResponseEntity.ok(example.calculateSomething());
}
...
}
@Entity
@PrimaryKeyJoinColumn(name = "five")
public class Example extends Foo {
@Column
@NotNull(groups = ValidOne.class)
private Integer one;
@Column
@NotNull(groups = ValidTwo.class)
private Integer two;
....
}
英文:
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
public interface ValidOne {
}
public interface ValidTwo {
}
public class SomeController {
@PostMapping ("/calculateSomeNumber")
public ResponseEntity calculateSomeNumber(@Validated({ValidOne.class}) @RequestBody Example example){
return ResponseEntity.ok(example.calculateSomething());
}
...
@Entity
@PrimaryKeyJoinColumn(name = "five")
public class Example extends Foo {
@Column
@NotNull(groups = ValidOne.class)
private Integer one;
@Column
@NotNull(groups = ValidTwo.class)
private Integer two;
....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论