Hello
Is there a way to implement a custom validation for JAX-RS API? (something similar to what we have in Spring @Valid & @Validated. I have some cases where the API is throwing 500 status code when instead of a list, I send a string, or I send a wrong enum value.
To implement custom validation for a JAX-RS API, We can use the BeanValidationProvider and JAXRSBeanValidationInInterceptor from Apache CXF
1 . Define the Validation Provider: Create a bean for the BeanValidationProvider which will be used to validate your JAX-RS endpoint beans. <bean id="validationProvider" class="org.apache.cxf.validation.BeanValidationProvider" />
2. Create the Validation Interceptor: Define a bean for the JAXRSBeanValidationInInterceptor and set the validationProvider as its property.
3 . Configure the JAX-RS Service:
Set up your JAX-RS service to use the validation interceptor. This involves configuring the JAXRSServerFactoryBean and adding the validationInInterceptor to its list of interceptors.
This setup ensures that your JAX-RS endpoints are validated using the specified validation provider and interceptor. If you encounter any issues, such as missing dependencies or configuration errors, ensure that all necessary dependencies for Jakarta Bean Validation and Apache CXF are included in your project.