'Validate different POST requests, one endpoint

Two types of POST requests are sent to the "/numbers" endpoint:

{
    "type": "A",
    "numberOne": 1
}

and

{
    "type": "B",
    "numberOne": 1,
    "numberTwo": 2
}

How to validate for type A that numberOne is not null and for type B that numberOne and numberTwo are not null? I was able to find explanations of how to achieve this with two different endpoints and validation groups, but not how to achieve this with one endpoint.

@RestController
public class NumbersController {

    @PostMapping("/numbers")
    public Numbers addNumbers(@Valid @RequestBody Numbers numbers) {
        // Create data. 
        return numbers;
    }
}

@Data
public class Numbers {
    private String type;
    @NotNull
    private Integer numberOne;
    @NotNull
    private Integer numberTwo;
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source