Hello there,
I've been trying to get validation to trigger for outward HTTP calls in the context of @HttpExchange
interfaces.
A missing path variable, mandatory request parameter or request body will throw an IllegalArgumentException
, but no ConstraintViolationException
is thrown when Jakarta constraints are not respected
I found this ticket which seems to indicate that this was raised and implemented in Jan 2023: https://github.com/spring-projects/spring-framework/issues/29782
However, it seems to not be working, so I've built a small app reproducing the problem attached to this issue.
Thanks in advance
Comment From: rstoyanchev
You need MethodValidationPostProcessor
to apply validation via AOP, and the HTTP interface to be a bean:
@Bean
DemoExchange demoExchange() {
return HttpServiceProxyFactory
.builderFor(WebClientAdapter.create(WebClient.builder().build()))
.build()
.createClient(DemoExchange.class);
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
@Bean
public ApplicationRunner commandLineRunner(DemoExchange demoExchange) {
return args -> {
try {
System.out.println("validation didn't work: " + demoExchange.get(1));
}
catch (ConstraintViolationException e) {
System.out.println("validation worked: " + e.getMessage());
}
};
}
Comment From: GuillaumeVT
Thanks @rstoyanchev, It worked with the exchange being a bean.
For the record, I didn't have to add the MethodValidationPostProcessor to make it work