Custom RestController Bean Validation

Hi All…

I wish to use bean validation annotations on parameters of methods of a custom @RestController … it seems like this should be supported when controller method marked @Validated, but it is not working… is this to be expected?
resttest.zip (419.3 KB)

Attached is sample project with a sum method with min/max constraints on first parameter.

When service called via restapi addon, constraint is enforced:

@Validated
public Integer sum(@Min(4) @Max(10) Integer number1, Integer number2) {
    return number1+number2;
}

http://localhost:8080/app/rest/v2/services/resttest_TestService/sum?number1=17&number2=34
{
“message”: “must be less than or equal to 10”,
“messageTemplate”: “{javax.validation.constraints.Max.message}”,
“path”: “sum.arg0”,
“invalidValue”: 17
}

but when custom controller method with same constraint called, it is not caught:

public class MyController {

@GetMapping("/func")
@Validated
public Integer sum(@Min(4) @Max(10) Integer number1, Integer number2) {
    return number1+number2;
}

}
http://localhost:8080/app/rest/myapi/func?number1=17&number2=3
20

Is there some straightforward way to get validation on the custom controller?

Hi,
CUBA isn’t based on Spring Boot, it uses vanilla Spring configuration.
So you cannot apply recipes available for Spring Boot, you may need to declare some beans or declare some XML configuration.

I would try to configure validator using one of recipes from these documentation chapters:
https://docs.spring.io/spring-framework/docs/5.2.25.RELEASE/spring-framework-reference/core.html#validation-beanvalidation
or
https://docs.spring.io/spring-framework/docs/5.2.25.RELEASE/spring-framework-reference/web.html#mvc-config-validation

Thank you for this response, it was helpful.