Following up on this discussion about disabling the built-in controller method validation here with @rstoyanchev.
The bean validation spec rule seems reasonable for polymorphic use, but controllers are invoked directly by the web framework, and I'm not sure there is a practical difference there.
Sorry, I don't think I fully understand your point here. Do you think it would be reasonable that if the validation annotation is present only on the interface, but not on the controller, that no validation is performed? This would work well for my problem as I could place the annotations only on the controller that should actually perform validation and not on the one in the proxy (this is in line with the spec I think). Currently validation is performed even if the annotation is only in the interface.
I'm not sure it would be ideal to have annotations specifically to turn off method validation.
I see your point, maybe there is another solution that does not require a new validation annotation? Would it be possible to specify the Validator
implementation per controller? I could use a no-op Validator
for some controllers that don't need validation and the Spring Validator
for the other controllers.
If your proxy does not need validation at all
Unfortunately this doesn't work as there are some controllers that do use validation.
Thank you!
Comment From: rstoyanchev
Sorry, I don't think I fully understand your point here. Do you think it would be reasonable that if the validation annotation is present only on the interface, but not on the controller, that no validation is performed?
I think you mean the opposite, if validation annotations are present only on the controller, but not on the interface?
Generally, in an object hierarchy, it makes sense for preconditions to be in the contract so that code using the contract works just the same with any implementation. Controllers are different in that they are never used hidden behind a contract. They are just entry points for web handling, only ever invoked by Spring MVC, which is fully aware of both contract and concrete implementation.
This would work well for my problem as I could place the annotations only on the controller that should actually perform validation and not on the one in the proxy (this is in line with the spec I think). Currently validation is performed even if the annotation is only in the interface.
That would be my suggestion, and it makes sense given that validation should be performed for one set of controllers only.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: mlichtblau
Sorry for the late reply.
Generally, in an object hierarchy, it makes sense for preconditions to be in the contract so that code using the contract works just the same with any implementation. Controllers are different in that they are never used hidden behind a contract. They are just entry points for web handling, only ever invoked by Spring MVC, which is fully aware of both contract and concrete implementation.
I fully agree that this is a special corner case relevant only to controllers. Sorry if my problem description was unclear.
The issue is that it is not possible to put the constraint annotation only on the controller when it inherits from an interface that does not have the annotation. In this case a runtime exception will be thrown, indicating that the 4.5.5 rule of the bean validation spec is violated. This means I have to put the validation also on the interface.
Example from docs
Example 4.16. Illegally declared parameter constraints on interface implementationpublic interface OrderService {
void placeOrder(String customerCode, Item item, int quantity);
}
public class SimpleOrderService implements OrderService {
@Override
public void placeOrder(
@NotNull @Size(min=3, max=20) String customerCode,
@NotNull Item item,
@Min(1) int quantity) {
[...]
}
}
> The constraints in SimpleOrderService are illegal, as they strengthen the preconditions of the placeOrder() method as constituted by the interface OrderService.
This will throw a runtime exception.
My proxy controller also inherits from this interface, but even though the annotation is not present on the controller, from what I can tell validation is still performed because of the annotation in the interface. This is also mentioned in the linked docs.
This means there is no possibility for me to place annotations somewhere to achieve me desired outcome while keeping the current inheritance structure of one interface and both controllers inheriting from that interface.
This is why I would like to disable all validation for one of these controllers.