2

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());
    }
xXsar_toXx
  • 23
  • 4

1 Answers1

2

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;

....
SKumar
  • 1,940
  • 1
  • 7
  • 12
  • So if I e.g. only want to validate the Integers „one“, „two“ and „three“ I have to put them in this validation group. Because when I did research it said that those groups would only set the order How all the values are going to be validated. So it would give me an exception if the Integers „four“ and „five“ are null. But I’ll try it out tomorrow thanks for the reply. – xXsar_toXx Aug 09 '20 at 09:23
  • Yes, that's right. You can keep Integerone, two and three and activate a specific group. – SKumar Aug 09 '20 at 09:31
  • I just took a second look and the problem with @GroupSequence above the whole Rest-Controller class is, that i only want this kind of validation for one endpoint in that rest controller class, and not for all endpoints – xXsar_toXx Aug 10 '20 at 07:46
  • @xXsar_toXx I updated my answer, I think replacing Valid with Validated should just do what is needed. – SKumar Aug 10 '20 at 09:33
  • I just changed it to Validated in the parameter and it works. Thanks for your help – xXsar_toXx Aug 10 '20 at 10:12
  • So I have another question. If I have a custom Object in the class Example, how can I apply the validations from the group sequence to the annotated values in the "example" object and the nested object in there (of course the values in the nested Object are also annotated and assigned to this group)? – xXsar_toXx Aug 11 '20 at 14:07
  • @xXsar_toXx Is it not working when that Custom Object is also marked with Valid annotation ? – SKumar Aug 11 '20 at 15:39
  • @sKumer Oh I didn’t know that it also has to be marked valid in the example class to work. Do I have to specify something else other than that? – xXsar_toXx Aug 12 '20 at 16:09
  • Alright now everything works out with the @Valid before the nested object thank you – xXsar_toXx Aug 17 '20 at 09:20