13

I am trying to use javax.validation.validation-api for validating @QueryParam parameters. I have followed steps as below:

  1. Added dependency:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-bean-validation</artifactId>
        <version>2.12</version>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  2. Jersey resource:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{param1}")
    @ValidateOnExecution
    public SomeObject get(@PathParam("param1") String param1,
        @NotNull @QueryParam("q1") String q1,
        @NotNull @QueryParam("q2") String q2) {
    
        try {
            //Assuming q1 and q2 are NOT Null here
            ...
        } catch(Exception exception) { 
            throw new WebApplicationException(exception, 
                    Response.Status.INTERNAL_SERVER_ERROR);
        }
        return someObject;
    }     
    
  3. I tried giving various combination of URLs as in q1 and q2 both parameters absent, q1 absent or q2 absent. Each time, @NotNull is not getting detected. Means try block code is getting executed irrespective of q1 and q2 being null.

What else needs to be done?

I referred this link - https://jersey.java.net/documentation/latest/bean-validation.html#d0e11956.

How to check Auto-Discoverable feature is on in my environment?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Bruso
  • 803
  • 3
  • 12
  • 25
  • 1
    Did you check to make sure you haven't set any of these: `CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE`, `ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE`,`ServerProperties.BV_FEATURE_DISABLE`? – Giovanni Botta Sep 09 '14 at 13:09
  • where to check these properties? – Bruso Sep 09 '14 at 13:21
  • In your Jersey configuration, some class extending `ResourceConfig` or `Application`, e.g., [as described here](https://jersey.java.net/documentation/latest/user-guide.html#deployment.servlet.3). – Giovanni Botta Sep 09 '14 at 17:39
  • thanks, I could find these settings in my environment and ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE is TRUE so BeanValidation is not working. Is it possible to set ServerProperties.BV_FEATURE_DISABLE as FALSE keeping FEATURE_AUTO_DISCOVERY_DISABLE as TRUE as it is? – Bruso Sep 10 '14 at 10:35
  • 2
    You are excluding Hibernate Validator from the dependencies. To make this work you need to add an alternative Bean Validation implementation. Do you? It is not shown in your example. What happen, if you remove the exclusion? – Hardy Sep 10 '14 at 12:43
  • @PST You will have to register the bean validation feature manually. Go dig into the Jersey docs/validation code to figure out how. – Giovanni Botta Sep 10 '14 at 14:43
  • @Hardy haven't used this version of Jersey but that came from the docs so I'm not sure why it's there. If it is a problem it's a second order problem though. – Giovanni Botta Sep 10 '14 at 14:44
  • The docs says:"If you want to use a different implementation of the Bean Validation API, use standard Maven mechanisms to exclude Hibernate Validator from the modules dependencies and add a dependency of your own. " - So unless you don't want to exclude Hibernate Validator in order to add a different implementation, you should remove the exclude. As it stands now you just include the API w/o any actual Bean Validation implementation. – Hardy Sep 10 '14 at 18:17
  • @GiovanniBotta you are right, I can set this ServerProperties.BV_FEATURE_DISABLE as FALSE manually. Let me try this. – Bruso Sep 11 '14 at 04:59
  • @Hardy I would like to add my own implementation so excluding Hibernate dependency. I have added implementation of javax.validation.ConstraintValidator. – Bruso Sep 11 '14 at 05:02
  • You need more than an implementation of a ConstraintValidator. You need the actual Bean Validation provider implementation. Implementing and providing custom constraints and constraint validators is just a Bean Validation extension point. Your Bean Validation provider is going to pick this up and add them to the default constraints. Like it or not, you need a Bean Validation implementation/provider on the classpath. – Hardy Sep 11 '14 at 13:17
  • I used bean validation with Jersey in the past and I believe I just used the default bean validation implementation (the Hibernate one I believe). I don't see a reason to implement your own. You just need to handle the exceptions thrown in case of validation errors with appropriate `ExceptionMapper`(s). – Giovanni Botta Sep 11 '14 at 15:01
  • I asked a [question about bean validation](http://stackoverflow.com/questions/22567097/jersey-bean-validation-parameternameprovider) that might also be helpful to you down the line. – Giovanni Botta Sep 11 '14 at 15:02
  • I'm afraid `jersey-bean-validation` is xml validator which is for web services and you're using restfull api, correct me please if I'm wrong. – deathangel908 Oct 06 '15 at 09:37
  • Yes I am using it within JAX-RS resource. As per suggestion in this post, I tried to include Hibernate Validator. Still @NotNull does not recognize the null arguments. – Bruso Oct 06 '15 at 10:37
  • Can you share your web.xml or your ResourceConfig implementation/extension? – Aritra Nov 19 '15 at 00:17

0 Answers0