-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable built-in controller method validation per controller #32292
Comments
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.
That would be my suggestion, and it makes sense given that validation should be performed for one set of controllers only. |
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. |
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. |
Sorry for the late reply.
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 docsExample 4.16. Illegally declared parameter constraints on interface implementation public 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) {
[...]
}
}
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. |
Following up on this discussion about disabling the built-in controller method validation here with @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? 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 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-opValidator
for some controllers that don't need validation and the SpringValidator
for the other controllers.Unfortunately this doesn't work as there are some controllers that do use validation.
Thank you!
The text was updated successfully, but these errors were encountered: