Discussion:
Enabling bean validation
(too old to reply)
Oliver B. Fischer
2014-04-23 06:50:55 UTC
Permalink
Hi,

I try to enable bean validation i a test application with Jersey 2.7. I
added org.glassfish.jersey.ext:jersey-bean-validation as dependency and
called

register(ValidationFeature.class);

But if I call

isEnabled(ValidationFeature.class);

it returns false.

So what did I miss? According to the documentation Jersey will enable
bean validation if it finds the right dependencies in the classpath.

BR, Oliver
Michal Gajdos
2014-04-23 07:40:57 UTC
Permalink
Hi Oliver,

where exactly are you trying to call isEnabled method? The method
returns true if ValidationFeature is registered in an application (it's
enough to add jersey-bean-validation on the class-path) AND the
ValidationFeature has been processed (ValidationFeature.configure has
been invoked - this happens during initialization of the app). This
means that if you call isEnabled in ResourceConfig it would return false
(ValidationFeature has not been processed). Similarly this the return
value would be false if you call the method in Features that are
processed BEFORE ValidationFeature. The method would return true when a
request is processed (i.e. in resource method, filters, interceptors, ...).

HTH,
Michal
Post by Oliver B. Fischer
Hi,
I try to enable bean validation i a test application with Jersey 2.7.
I added org.glassfish.jersey.ext:jersey-bean-validation as dependency
and called
register(ValidationFeature.class);
But if I call
isEnabled(ValidationFeature.class);
it returns false.
So what did I miss? According to the documentation Jersey will enable
bean validation if it finds the right dependencies in the classpath.
BR, Oliver
Oliver B. Fischer
2014-04-23 14:29:22 UTC
Permalink
Hi Michal,

thanks for the hints. I called isEnabled() always in the ctor of the
application and now it is clear why it returnd false. Now I obtain the
configuration via

@Context Configuration config

Afterwards I can call config.isEnabled(ValidationFeature.class) and it
returns true. That is great.

One question more: How do I ensure that an entity will be validated
before it will be overhanded to a resource method? I use
jersey-media-json-jackson to map JSON to an annotated entity. But the
entity is never validated. At least it seams so to me. The class is
annotated with some @NotNull annotations but I never get constraint
validations.

BTW, the code is online at
https://bitbucket.org/obfischer/jersey-playground/src/0703ae28873a/beanvalidation-simple/src/main/java/net/sweblog/jerseyplayground/beanvalidationsimple/b/?at=master
available.

Thanks,

Oliver
Post by Michal Gajdos
Hi Oliver,
where exactly are you trying to call isEnabled method? The method
returns true if ValidationFeature is registered in an application (it's
enough to add jersey-bean-validation on the class-path) AND the
ValidationFeature has been processed (ValidationFeature.configure has
been invoked - this happens during initialization of the app). This
means that if you call isEnabled in ResourceConfig it would return false
(ValidationFeature has not been processed). Similarly this the return
value would be false if you call the method in Features that are
processed BEFORE ValidationFeature. The method would return true when a
request is processed (i.e. in resource method, filters, interceptors, ...).
HTH,
Michal
Post by Oliver B. Fischer
Hi,
I try to enable bean validation i a test application with Jersey 2.7.
I added org.glassfish.jersey.ext:jersey-bean-validation as dependency
and called
register(ValidationFeature.class);
But if I call
isEnabled(ValidationFeature.class);
it returns false.
So what did I miss? According to the documentation Jersey will enable
bean validation if it finds the right dependencies in the classpath.
BR, Oliver
Michal Gajdos
2014-04-23 14:43:46 UTC
Permalink
Hi Oliver,

this area is described in JAX-RS 2.0 spec [1], chapter 7.3 Entity
Validation or in the BV spec [2]. Basically you need to tell underlying
BV engine to validate an input param (via @Valid or via specialized
constraint annotation). In your case @Valid would be enough. Take a look
at the example from the JAX-RS spec:

JAX-RS: Java API for RESTful Web Services @Path("/")
class MyResourceClass {

@POST
@Consumes("application/xml")
public void registerUser(@Valid User user) { ... }
}

[1] https://jcp.org/aboutJava/communityprocess/final/jsr339/index.html
[2] http://beanvalidation.org/1.1/spec/

Michal
Post by Oliver B. Fischer
Hi Michal,
thanks for the hints. I called isEnabled() always in the ctor of the
application and now it is clear why it returnd false. Now I obtain the
configuration via
@Context Configuration config
Afterwards I can call config.isEnabled(ValidationFeature.class) and it
returns true. That is great.
One question more: How do I ensure that an entity will be validated
before it will be overhanded to a resource method? I use
jersey-media-json-jackson to map JSON to an annotated entity. But the
entity is never validated. At least it seams so to me. The class is
validations.
BTW, the code is online at
https://bitbucket.org/obfischer/jersey-playground/src/0703ae28873a/beanvalidation-simple/src/main/java/net/sweblog/jerseyplayground/beanvalidationsimple/b/?at=master
available.
Thanks,
Oliver
Post by Michal Gajdos
Hi Oliver,
where exactly are you trying to call isEnabled method? The method
returns true if ValidationFeature is registered in an application (it's
enough to add jersey-bean-validation on the class-path) AND the
ValidationFeature has been processed (ValidationFeature.configure has
been invoked - this happens during initialization of the app). This
means that if you call isEnabled in ResourceConfig it would return false
(ValidationFeature has not been processed). Similarly this the return
value would be false if you call the method in Features that are
processed BEFORE ValidationFeature. The method would return true when a
request is processed (i.e. in resource method, filters, interceptors, ...).
HTH,
Michal
Post by Oliver B. Fischer
Hi,
I try to enable bean validation i a test application with Jersey 2.7.
I added org.glassfish.jersey.ext:jersey-bean-validation as dependency
and called
register(ValidationFeature.class);
But if I call
isEnabled(ValidationFeature.class);
it returns false.
So what did I miss? According to the documentation Jersey will enable
bean validation if it finds the right dependencies in the classpath.
BR, Oliver
Oliver B. Fischer
2014-04-23 14:53:32 UTC
Permalink
Now I got it. RTFM! ;-)

Thank you!
Post by Michal Gajdos
Hi Oliver,
this area is described in JAX-RS 2.0 spec [1], chapter 7.3 Entity
Validation or in the BV spec [2]. Basically you need to tell underlying
class MyResourceClass {
@POST
@Consumes("application/xml")
}
[1] https://jcp.org/aboutJava/communityprocess/final/jsr339/index.html
[2] http://beanvalidation.org/1.1/spec/
Michal
Post by Oliver B. Fischer
Hi Michal,
thanks for the hints. I called isEnabled() always in the ctor of the
application and now it is clear why it returnd false. Now I obtain the
configuration via
@Context Configuration config
Afterwards I can call config.isEnabled(ValidationFeature.class) and it
returns true. That is great.
One question more: How do I ensure that an entity will be validated
before it will be overhanded to a resource method? I use
jersey-media-json-jackson to map JSON to an annotated entity. But the
entity is never validated. At least it seams so to me. The class is
validations.
BTW, the code is online at
https://bitbucket.org/obfischer/jersey-playground/src/0703ae28873a/beanvalidation-simple/src/main/java/net/sweblog/jerseyplayground/beanvalidationsimple/b/?at=master
available.
Thanks,
Oliver
Loading...