You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Chris Christo <_c...@mail.com> on 2014/02/06 13:38:23 UTC
JAX-RS + Bean Validation
Hi guys,
Wondering if anyone has managed to get the current version of openejb (4.6.0/4.6.1) working with jax-rs 2.0 style validation? Or any workarounds using third party libs?
Basically I wanted to do something along the lines of:
@POST
@Path("/bounce")
@ValidateRequest
public Coffee bounce(@Valid Coffee coffee) {
return coffee;
}
where the (coffee) bean sent, is automatically checked for validity.
Regards,
Chris
Re: JAX-RS + Bean Validation
Posted by hwaastad <he...@waastad.org>.
Hi,
if U have MDB in your project U cannot define a system bval. U have to
define bval using ejb-jar.xml:
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.1"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<assembly-descriptor>
<interceptor-binding>
<ejb-name>DownloadService</ejb-name>
<interceptor-class>org.apache.openejb.bval.BeanValidationAppendixInterceptor</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
br hw
--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4671337.html
Sent from the TomEE Users mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by Nagesh <na...@enhancesys.com>.
Hi,
Can you provide ejb-jar.xml format in detail. I am getting similar error.
Re: JAX-RS + Bean Validation
Posted by Romain Manni-Bucau <rm...@gmail.com>.
well you need to explode javaee-api (explained here
http://rmannibucau.wordpress.com/2013/07/19/little-tip-to-help-you-to-test-javaee-7-in-tomee-with-tomee-maven-plugin/
) and replace bval jars by the bval implementation you want (Apache
BVal 1.1 is not yet released but you can build it from sources
http://svn.apache.org/repos/asf/bval/branches/bval-11/ then just use
bval-jsr and bval-core jars) + the bval api 1.1
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau
2014-02-07 hwaastad <he...@waastad.org>:
> Wow,
> first of all thx. ejb-jar worked fine.
>
> So, if I wanna try out bval 1.1, which jars needs to be replaced in TomEE?
>
> br hw
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4667527.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by hwaastad <he...@waastad.org>.
Wow,
first of all thx. ejb-jar worked fine.
So, if I wanna try out bval 1.1, which jars needs to be replaced in TomEE?
br hw
--
View this message in context: http://openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4667527.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by Romain Manni-Bucau <rm...@gmail.com>.
btw just commited a fix about it but in javaee 7 or if you use bval
1.1 in tomee it will activate this feature automatically
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau
2014-02-07 Romain Manni-Bucau <rm...@gmail.com>:
> the interceptor is not designed for mdbs. Just configure the
> interceptor in ejb-jar instead of using the system property
>
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
>
> 2014-02-07 hwaastad <he...@waastad.org>:
>> Nice reading and thanks for sharing :-)
>>
>> I tried adding system.properties entry, however I discovered a challenge:
>>
>> ---
>>
>> SEVERE: EjbTransactionUtil.handleSystemException:
>> org.apache.openejb.core.mdb.MdbContext cannot be cast to
>> javax.ejb.SessionContext
>> java.lang.ClassCastException: org.apache.openejb.core.mdb.MdbContext cannot
>> be cast to javax.ejb.SessionContext
>> at
>> org.apache.openejb.bval.BeanValidationAppendixInterceptor.aroundInvoke(BeanValidationAppendixInterceptor.java:53)
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>> at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>> at java.lang.reflect.Method.invoke(Method.java:606)
>> at
>> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:182)
>> at
>> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:164)
>> at
>> org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:180)
>> at
>> org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:99)
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>> at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>> at java.lang.reflect.Method.invoke(Method.java:606)
>> at
>> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:182)
>> at
>> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:164)
>> at
>> org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:80)
>> at org.apache.openejb.core.mdb.MdbContainer._invoke(MdbContainer.java:401)
>> at org.apache.openejb.core.mdb.MdbContainer.invoke(MdbContainer.java:372)
>> at
>> org.apache.openejb.core.mdb.EndpointHandler.deliverMessage(EndpointHandler.java:227)
>> at
>> org.apache.openejb.core.mdb.EndpointHandler.invoke(EndpointHandler.java:168)
>> at
>> org.waastad.activemq.jms.SmartProbeMessageBean$$LocalBeanProxy.onMessage(org/waastad/activemq/jms/SmartProbeMessageBean.java)
>> at
>> org.apache.activemq.ra.MessageEndpointProxy$MessageEndpointAlive.onMessage(MessageEndpointProxy.java:123)
>> at
>> org.apache.activemq.ra.MessageEndpointProxy.onMessage(MessageEndpointProxy.java:64)
>> at org.apache.activemq.ActiveMQSession.run(ActiveMQSession.java:916)
>> at org.apache.activemq.ra.ServerSessionImpl.run(ServerSessionImpl.java:169)
>> at
>> org.apache.geronimo.connector.work.WorkerContext.run(WorkerContext.java:366)
>> at
>> org.apache.geronimo.connector.work.pool.NamedRunnable.run(NamedRunnable.java:32)
>> at
>> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
>> at
>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
>> at java.lang.Thread.run(Thread.java:744)
>>
>> ----
>>
>> @Romain: what might cause this issue?
>>
>> TomEE 1.6.0
>>
>> br hw
>>
>>
>>
>> --
>> View this message in context: http://openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4667523.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by Romain Manni-Bucau <rm...@gmail.com>.
the interceptor is not designed for mdbs. Just configure the
interceptor in ejb-jar instead of using the system property
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau
2014-02-07 hwaastad <he...@waastad.org>:
> Nice reading and thanks for sharing :-)
>
> I tried adding system.properties entry, however I discovered a challenge:
>
> ---
>
> SEVERE: EjbTransactionUtil.handleSystemException:
> org.apache.openejb.core.mdb.MdbContext cannot be cast to
> javax.ejb.SessionContext
> java.lang.ClassCastException: org.apache.openejb.core.mdb.MdbContext cannot
> be cast to javax.ejb.SessionContext
> at
> org.apache.openejb.bval.BeanValidationAppendixInterceptor.aroundInvoke(BeanValidationAppendixInterceptor.java:53)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:606)
> at
> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:182)
> at
> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:164)
> at
> org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:180)
> at
> org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:99)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:606)
> at
> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:182)
> at
> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:164)
> at
> org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:80)
> at org.apache.openejb.core.mdb.MdbContainer._invoke(MdbContainer.java:401)
> at org.apache.openejb.core.mdb.MdbContainer.invoke(MdbContainer.java:372)
> at
> org.apache.openejb.core.mdb.EndpointHandler.deliverMessage(EndpointHandler.java:227)
> at
> org.apache.openejb.core.mdb.EndpointHandler.invoke(EndpointHandler.java:168)
> at
> org.waastad.activemq.jms.SmartProbeMessageBean$$LocalBeanProxy.onMessage(org/waastad/activemq/jms/SmartProbeMessageBean.java)
> at
> org.apache.activemq.ra.MessageEndpointProxy$MessageEndpointAlive.onMessage(MessageEndpointProxy.java:123)
> at
> org.apache.activemq.ra.MessageEndpointProxy.onMessage(MessageEndpointProxy.java:64)
> at org.apache.activemq.ActiveMQSession.run(ActiveMQSession.java:916)
> at org.apache.activemq.ra.ServerSessionImpl.run(ServerSessionImpl.java:169)
> at
> org.apache.geronimo.connector.work.WorkerContext.run(WorkerContext.java:366)
> at
> org.apache.geronimo.connector.work.pool.NamedRunnable.run(NamedRunnable.java:32)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
> at java.lang.Thread.run(Thread.java:744)
>
> ----
>
> @Romain: what might cause this issue?
>
> TomEE 1.6.0
>
> br hw
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4667523.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by hwaastad <he...@waastad.org>.
Nice reading and thanks for sharing :-)
I tried adding system.properties entry, however I discovered a challenge:
---
SEVERE: EjbTransactionUtil.handleSystemException:
org.apache.openejb.core.mdb.MdbContext cannot be cast to
javax.ejb.SessionContext
java.lang.ClassCastException: org.apache.openejb.core.mdb.MdbContext cannot
be cast to javax.ejb.SessionContext
at
org.apache.openejb.bval.BeanValidationAppendixInterceptor.aroundInvoke(BeanValidationAppendixInterceptor.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at
org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:182)
at
org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:164)
at
org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:180)
at
org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at
org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:182)
at
org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:164)
at
org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:80)
at org.apache.openejb.core.mdb.MdbContainer._invoke(MdbContainer.java:401)
at org.apache.openejb.core.mdb.MdbContainer.invoke(MdbContainer.java:372)
at
org.apache.openejb.core.mdb.EndpointHandler.deliverMessage(EndpointHandler.java:227)
at
org.apache.openejb.core.mdb.EndpointHandler.invoke(EndpointHandler.java:168)
at
org.waastad.activemq.jms.SmartProbeMessageBean$$LocalBeanProxy.onMessage(org/waastad/activemq/jms/SmartProbeMessageBean.java)
at
org.apache.activemq.ra.MessageEndpointProxy$MessageEndpointAlive.onMessage(MessageEndpointProxy.java:123)
at
org.apache.activemq.ra.MessageEndpointProxy.onMessage(MessageEndpointProxy.java:64)
at org.apache.activemq.ActiveMQSession.run(ActiveMQSession.java:916)
at org.apache.activemq.ra.ServerSessionImpl.run(ServerSessionImpl.java:169)
at
org.apache.geronimo.connector.work.WorkerContext.run(WorkerContext.java:366)
at
org.apache.geronimo.connector.work.pool.NamedRunnable.run(NamedRunnable.java:32)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
----
@Romain: what might cause this issue?
TomEE 1.6.0
br hw
--
View this message in context: http://openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4667523.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by ChrisChristo <_c...@mail.com>.
Wow, that actually worked. Thanks.
So for anyone else wanting to use the @Valid on their method params simply
add the following property to your system properties.
org.apache.openejb.default.system.interceptors =
org.apache.openejb.bval.BeanValidationAppendixInterceptor
And then your jax-rs methods can be of the following (note that you don’t
need the @ValidateRequest):
@POST
@Path("/bounce")
public Coffee bounce(@Valid Coffee coffee) {
return coffee;
}
If the Coffee bean sent has any violations (such as a field within being
null when a @NotNull annotation is brandished) then a
javax.validation.ConstraintViolationException will be thrown.
Simply pick that up by your exception mapper and do what you want with it,
so something along the lines of:
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyExceptionMapper implements ExceptionMapper<Exception> {
if (ex instanceof ConstraintViolationException) {
// return back nice json
return ExceptionResponse.build(new
IllegalArgumentException(ExceptionResponse.MALFORMED_BODY),
Response.Status.BAD_REQUEST);
}
}
(here ExceptionResponse is my own custom class for representing exceptions
in a nice format back to the caller).
Remember to add your MyExceptionMapper to your system properties also:
"cxf.jaxrs.providers": “my.package.MyExceptionMapper”
If you use openejb.json (in conf/openejb.json) or resources.json (in
project’s resources/META-INF/resources.json) then you can add the two
properties like so:
resources.json/open.json :
{
"system-properties": {
"cxf.jaxrs.providers": “my.package.MyExceptionMapper",
"org.apache.openejb.default.system.interceptors":
"org.apache.openejb.bval.BeanValidationAppendixInterceptor”,
"org.apache.openejb.cxf.bus.features":
"org.apache.cxf.feature.LoggingFeature"
}
// other stuff….
}
(The LoggingFeature is to log the requests and responses in a pretty format,
thank me later :P)
Hope this helps someone in the future.
Thanks again Romain,
Chris
--
View this message in context: http://openejb.979440.n4.nabble.com/JAX-RS-Bean-Validation-tp4667501p4667504.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: JAX-RS + Bean Validation
Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi
did you try adding:
org.apache.openejb.default.system.interceptors =
org.apache.openejb.bval.BeanValidationAppendixInterceptor in
conf/system.properties?
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau
2014-02-06 Chris Christo <_c...@mail.com>:
> Hi guys,
>
> Wondering if anyone has managed to get the current version of openejb (4.6.0/4.6.1) working with jax-rs 2.0 style validation? Or any workarounds using third party libs?
>
> Basically I wanted to do something along the lines of:
>
> @POST
> @Path("/bounce")
> @ValidateRequest
> public Coffee bounce(@Valid Coffee coffee) {
> return coffee;
> }
>
> where the (coffee) bean sent, is automatically checked for validity.
>
> Regards,
>
> Chris