You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Nathaniel Auvil <na...@gmail.com> on 2009/06/30 03:57:18 UTC

JAX-RS Exception Handlers and Validation

I am looking at how to get validations and exception handling going.  Right
now i send some garbage XML in and it throws a JAXException back in the
response.  I would prefer to be able to handle this back on the serve and
give the client a less specific error.

I am using Spring to configure things and i did not see anything in the
examples which shows me how to add an exception handler or how to get
control of the XML Validations.  Can anyone point me in the right direction?

Re: JAX-RS Exception Handlers and Validation

Posted by Nathaniel Auvil <na...@gmail.com>.
Yes this worked.  Thanks.


On Tue, Jun 30, 2009 at 9:22 AM, Sergey Beryozkin <sb...@progress.com>wrote:

>
>> Let me give a little background....i have a client passing XML to my
>> JAX-RS
>> service.  I am using JAXB bindings.  I appended some extra characters to
>> the
>> XML which i sent the service to see how it would error, in particular what
>> the client would get as a response.  The client got a JAXBException of
>> some
>> sort...can not remember from yesterday afternoon ;).
>>
>> If i register an ExceptionMapper for JAXBException or any of the XML
>> Validation exceptions i see, would that allow me to make a custom error
>> message to send back to the caller?
>>
>
> Yes, sure. JAXBExceptions are wrapped into WebApplicationExceptions but you
> can get to the cause exception, you need to register
> ExceptionMapper<WebApplicationException>. Alternatively, you might want to
> register a ValidationEventHandler instance  with JAXBElementProvider and
> when it's notified of the validation issues you can then throw some custom
> CustomValidationException and register
> ExceptionMapper<CustomValidationException>, you can register
> ValidationEventHandler as a 'validationEventHandler' property on
> org.apache.cxf.jaxrs.providers.JAXBElementProvider.
>
> Does it help ?
>
> Sergey
>
>
>
>>
>>
>>
>>
>>> cheers, Sergey
>>>
>>>
>>>
>>>
>>>>
>>>>
>>>> On Tue, Jun 30, 2009 at 5:35 AM, Sergey Beryozkin <
>>>> sberyozk@progress.com
>>>> >wrote:
>>>>
>>>>  Hi,
>>>>
>>>>>
>>>>> You can register an exception mapper as a provider from Spring, inside
>>>>> a
>>>>> jaxrs:providers tag.
>>>>>
>>>>> As far as the validation is concerned, here is how you can do it :
>>>>>
>>>>>
>>>>>
>>>>> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Schemavalidationsupport
>>>>>
>>>>> You can also inject an instance of ValidationEventHandler into a JAXB
>>>>> provider.
>>>>> Or you can get more control by registering a custom XMLStreamReader and
>>>>> do
>>>>> some schematron-like validation in there...and throw some exception
>>>>> from
>>>>> there....Let me know please if you'd like some more info on this last
>>>>> option.
>>>>>
>>>>> cheers, Sergey
>>>>>
>>>>>
>>>>> ----- Original Message ----- From: "Nathaniel Auvil" <
>>>>> nathaniel.auvil@gmail.com>
>>>>> To: <us...@cxf.apache.org>
>>>>> Sent: Tuesday, June 30, 2009 2:57 AM
>>>>> Subject: JAX-RS Exception Handlers and Validation
>>>>>
>>>>>
>>>>>
>>>>>  I am looking at how to get validations and exception handling going.
>>>>>
>>>>>   Right
>>>>>> now i send some garbage XML in and it throws a JAXException back in
>>>>>> the
>>>>>> response.  I would prefer to be able to handle this back on the serve
>>>>>> and
>>>>>> give the client a less specific error.
>>>>>>
>>>>>> I am using Spring to configure things and i did not see anything in
>>>>>> the
>>>>>> examples which shows me how to add an exception handler or how to get
>>>>>> control of the XML Validations.  Can anyone point me in the right
>>>>>> direction?
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>

Re: JAX-RS Exception Handlers and Validation

Posted by Sergey Beryozkin <sb...@progress.com>.
>
> Let me give a little background....i have a client passing XML to my JAX-RS
> service.  I am using JAXB bindings.  I appended some extra characters to the
> XML which i sent the service to see how it would error, in particular what
> the client would get as a response.  The client got a JAXBException of some
> sort...can not remember from yesterday afternoon ;).
>
> If i register an ExceptionMapper for JAXBException or any of the XML
> Validation exceptions i see, would that allow me to make a custom error
> message to send back to the caller?

Yes, sure. JAXBExceptions are wrapped into WebApplicationExceptions but you can get to the cause exception, you need to register 
ExceptionMapper<WebApplicationException>. Alternatively, you might want to register a ValidationEventHandler instance  with 
JAXBElementProvider and when it's notified of the validation issues you can then throw some custom CustomValidationException and 
register ExceptionMapper<CustomValidationException>, you can register ValidationEventHandler as a 'validationEventHandler' property 
on org.apache.cxf.jaxrs.providers.JAXBElementProvider.

Does it help ?

Sergey

>
>
>
>
>>
>> cheers, Sergey
>>
>>
>>
>>>
>>>
>>>
>>> On Tue, Jun 30, 2009 at 5:35 AM, Sergey Beryozkin <sberyozk@progress.com
>>> >wrote:
>>>
>>>  Hi,
>>>>
>>>> You can register an exception mapper as a provider from Spring, inside a
>>>> jaxrs:providers tag.
>>>>
>>>> As far as the validation is concerned, here is how you can do it :
>>>>
>>>>
>>>> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Schemavalidationsupport
>>>>
>>>> You can also inject an instance of ValidationEventHandler into a JAXB
>>>> provider.
>>>> Or you can get more control by registering a custom XMLStreamReader and
>>>> do
>>>> some schematron-like validation in there...and throw some exception from
>>>> there....Let me know please if you'd like some more info on this last
>>>> option.
>>>>
>>>> cheers, Sergey
>>>>
>>>>
>>>> ----- Original Message ----- From: "Nathaniel Auvil" <
>>>> nathaniel.auvil@gmail.com>
>>>> To: <us...@cxf.apache.org>
>>>> Sent: Tuesday, June 30, 2009 2:57 AM
>>>> Subject: JAX-RS Exception Handlers and Validation
>>>>
>>>>
>>>>
>>>>  I am looking at how to get validations and exception handling going.
>>>>
>>>>>  Right
>>>>> now i send some garbage XML in and it throws a JAXException back in the
>>>>> response.  I would prefer to be able to handle this back on the serve
>>>>> and
>>>>> give the client a less specific error.
>>>>>
>>>>> I am using Spring to configure things and i did not see anything in the
>>>>> examples which shows me how to add an exception handler or how to get
>>>>> control of the XML Validations.  Can anyone point me in the right
>>>>> direction?
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
> 


Re: JAX-RS Exception Handlers and Validation

Posted by Nathaniel Auvil <na...@gmail.com>.
On Tue, Jun 30, 2009 at 7:33 AM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
>
>  Thanks Sergey
>>
>> Can you give me an example?   Do i just add a <bean id=""  class="" /> of
>> my
>> ExceptionMapper implementation?
>>
>> <jaxrs:providers>
>> .....
>> </jaxrs:providers>
>>
>>  yes, exactly, or you can link to beans defined outside of
> jaxrs:providers, can be hamdy when you want to configure your providers
> somehow :
>
> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Registeringcustomproviders
>
>
>
>>
>>
>> I saw the validation example, but i was wondering how i can have at the
>> validator so i can massage the Response going back to the caller.
>>
>
> Can you clarify a bit more ? Would you like your ExceptionMapper to produce
> a custom Response if the validation error occurs ?
>

Let me give a little background....i have a client passing XML to my JAX-RS
service.  I am using JAXB bindings.  I appended some extra characters to the
XML which i sent the service to see how it would error, in particular what
the client would get as a response.  The client got a JAXBException of some
sort...can not remember from yesterday afternoon ;).

If i register an ExceptionMapper for JAXBException or any of the XML
Validation exceptions i see, would that allow me to make a custom error
message to send back to the caller?




>
> cheers, Sergey
>
>
>
>>
>>
>>
>> On Tue, Jun 30, 2009 at 5:35 AM, Sergey Beryozkin <sberyozk@progress.com
>> >wrote:
>>
>>  Hi,
>>>
>>> You can register an exception mapper as a provider from Spring, inside a
>>> jaxrs:providers tag.
>>>
>>> As far as the validation is concerned, here is how you can do it :
>>>
>>>
>>> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Schemavalidationsupport
>>>
>>> You can also inject an instance of ValidationEventHandler into a JAXB
>>> provider.
>>> Or you can get more control by registering a custom XMLStreamReader and
>>> do
>>> some schematron-like validation in there...and throw some exception from
>>> there....Let me know please if you'd like some more info on this last
>>> option.
>>>
>>> cheers, Sergey
>>>
>>>
>>> ----- Original Message ----- From: "Nathaniel Auvil" <
>>> nathaniel.auvil@gmail.com>
>>> To: <us...@cxf.apache.org>
>>> Sent: Tuesday, June 30, 2009 2:57 AM
>>> Subject: JAX-RS Exception Handlers and Validation
>>>
>>>
>>>
>>>  I am looking at how to get validations and exception handling going.
>>>
>>>>  Right
>>>> now i send some garbage XML in and it throws a JAXException back in the
>>>> response.  I would prefer to be able to handle this back on the serve
>>>> and
>>>> give the client a less specific error.
>>>>
>>>> I am using Spring to configure things and i did not see anything in the
>>>> examples which shows me how to add an exception handler or how to get
>>>> control of the XML Validations.  Can anyone point me in the right
>>>> direction?
>>>>
>>>>
>>>>
>>>
>>
>

Re: JAX-RS Exception Handlers and Validation

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi


> Thanks Sergey
>
> Can you give me an example?   Do i just add a <bean id=""  class="" /> of my
> ExceptionMapper implementation?
>
> <jaxrs:providers>
> .....
> </jaxrs:providers>
>
yes, exactly, or you can link to beans defined outside of jaxrs:providers, can be hamdy when you want to configure your providers 
somehow :
http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Registeringcustomproviders


>
>
>
> I saw the validation example, but i was wondering how i can have at the
> validator so i can massage the Response going back to the caller.

Can you clarify a bit more ? Would you like your ExceptionMapper to produce a custom Response if the validation error occurs ?

cheers, Sergey

>
>
>
>
> On Tue, Jun 30, 2009 at 5:35 AM, Sergey Beryozkin <sb...@progress.com>wrote:
>
>> Hi,
>>
>> You can register an exception mapper as a provider from Spring, inside a
>> jaxrs:providers tag.
>>
>> As far as the validation is concerned, here is how you can do it :
>>
>> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Schemavalidationsupport
>>
>> You can also inject an instance of ValidationEventHandler into a JAXB
>> provider.
>> Or you can get more control by registering a custom XMLStreamReader and do
>> some schematron-like validation in there...and throw some exception from
>> there....Let me know please if you'd like some more info on this last
>> option.
>>
>> cheers, Sergey
>>
>>
>> ----- Original Message ----- From: "Nathaniel Auvil" <
>> nathaniel.auvil@gmail.com>
>> To: <us...@cxf.apache.org>
>> Sent: Tuesday, June 30, 2009 2:57 AM
>> Subject: JAX-RS Exception Handlers and Validation
>>
>>
>>
>>  I am looking at how to get validations and exception handling going.
>>>  Right
>>> now i send some garbage XML in and it throws a JAXException back in the
>>> response.  I would prefer to be able to handle this back on the serve and
>>> give the client a less specific error.
>>>
>>> I am using Spring to configure things and i did not see anything in the
>>> examples which shows me how to add an exception handler or how to get
>>> control of the XML Validations.  Can anyone point me in the right
>>> direction?
>>>
>>>
>>
> 


Re: JAX-RS Exception Handlers and Validation

Posted by Nathaniel Auvil <na...@gmail.com>.
Thanks Sergey

Can you give me an example?   Do i just add a <bean id=""  class="" /> of my
ExceptionMapper implementation?

<jaxrs:providers>
.....
</jaxrs:providers>




I saw the validation example, but i was wondering how i can have at the
validator so i can massage the Response going back to the caller.




On Tue, Jun 30, 2009 at 5:35 AM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi,
>
> You can register an exception mapper as a provider from Spring, inside a
> jaxrs:providers tag.
>
> As far as the validation is concerned, here is how you can do it :
>
> http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Schemavalidationsupport
>
> You can also inject an instance of ValidationEventHandler into a JAXB
> provider.
> Or you can get more control by registering a custom XMLStreamReader and do
> some schematron-like validation in there...and throw some exception from
> there....Let me know please if you'd like some more info on this last
> option.
>
> cheers, Sergey
>
>
> ----- Original Message ----- From: "Nathaniel Auvil" <
> nathaniel.auvil@gmail.com>
> To: <us...@cxf.apache.org>
> Sent: Tuesday, June 30, 2009 2:57 AM
> Subject: JAX-RS Exception Handlers and Validation
>
>
>
>  I am looking at how to get validations and exception handling going.
>>  Right
>> now i send some garbage XML in and it throws a JAXException back in the
>> response.  I would prefer to be able to handle this back on the serve and
>> give the client a less specific error.
>>
>> I am using Spring to configure things and i did not see anything in the
>> examples which shows me how to add an exception handler or how to get
>> control of the XML Validations.  Can anyone point me in the right
>> direction?
>>
>>
>

Re: JAX-RS Exception Handlers and Validation

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi,

You can register an exception mapper as a provider from Spring, inside a jaxrs:providers tag.

As far as the validation is concerned, here is how you can do it :

http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Schemavalidationsupport

You can also inject an instance of ValidationEventHandler into a JAXB provider.
Or you can get more control by registering a custom XMLStreamReader and do some schematron-like validation in there...and throw some 
exception from there....Let me know please if you'd like some more info on this last option.

cheers, Sergey


----- Original Message ----- 
From: "Nathaniel Auvil" <na...@gmail.com>
To: <us...@cxf.apache.org>
Sent: Tuesday, June 30, 2009 2:57 AM
Subject: JAX-RS Exception Handlers and Validation


>I am looking at how to get validations and exception handling going.  Right
> now i send some garbage XML in and it throws a JAXException back in the
> response.  I would prefer to be able to handle this back on the serve and
> give the client a less specific error.
>
> I am using Spring to configure things and i did not see anything in the
> examples which shows me how to add an exception handler or how to get
> control of the XML Validations.  Can anyone point me in the right direction?
>