users@jersey.java.net

[Jersey] Re: Catch marshaling errors and present in one go

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Thu, 26 Mar 2015 17:56:00 +0100

Hi Naresh,

please see inline…

> On 26 Mar 2015, at 11:25, naresh <baliga_naresh_at_yahoo.com> wrote:
>
> Hi,
>
> I don't think bean validation will work, as it might never get to it due to the marshaling error encountered earlier on.

For marshaling case, validation will happen before you send data over the wire. So no problem here. But you are probably talking about
unmarshalling, and then you are right.


>
> Basically need to get a callback on the marshaling exception, do something, but more importantly Jersey needs to keep going on and not stop on the first exception.

Jersey does not do marshaling itself. It delegates to Jackson MBW. There is a possibility to configure Jackson within Jersey
so that it does not stick with the default behaviour:
https://jersey.java.net/documentation/latest/media.html#json.jackson <https://jersey.java.net/documentation/latest/media.html#json.jackson>
>
> The MessageBodyWorkers may work.
>
> What is the "normal" behavior in a REST POST or PUT though, where a user sends Strings that cannot marshal into the member variable data type in the POJO? Bail on the first bad marshal encountered? If that's normal, I guess that's what I might consider doing.

Ah, you mean unmarshaling. Anyway, if deserialization goes wrong you will get a 400/500 response back by default (depends on how bad the data are O:-)

~Jakub

>
>
> Sent from my T-Mobile 4G LTE Device
>
>
> -------- Original message --------
> From: "japod [via Jersey]" <[hidden email] <x-msg://9/user/SendEmail.jtp?type=node&node=7583223&i=0>>
> Date:03/26/2015 4:30 AM (GMT-06:00)
> To: naresh <[hidden email] <x-msg://9/user/SendEmail.jtp?type=node&node=7583223&i=1>>
> Cc:
> Subject: Re: Catch marshaling errors and present in one go
>
> Hi Naresh,
>
> I am not sure i completely understand you use case.
>
> However to better controll what happens when marshalling goes wrong
> you should be always able to register your custom message body provider,
> that would just delegate to the “original” one and process any exceptions being thrown.
> Please see the following doc for some details:
> https://jersey.java.net/documentation/latest/user-guide.html#d0e7605 <https://jersey.java.net/documentation/latest/user-guide.html#d0e7605>
> In the above doc, marshalling is done within a resource method, but the same
> pattern (MessageBodyWorkers usage) could be applied also within a custom
> message body reader/writer.
>
> Otherwise, the following doc discusses bean validation feature,
> that you might want to consider to apply to your use case instead:
> https://jersey.java.net/documentation/latest/user-guide.html#bean-validation <https://jersey.java.net/documentation/latest/user-guide.html#bean-validation>
>
> HTH,
>
> ~Jakub
>
>> On 26 Mar 2015, at 09:30, naresh <[hidden email] <x-msg://9/user/SendEmail.jtp?type=node&node=7583222&i=0>> wrote:
>>
>> Hi,
>> I'm using Jersey 2.17 and am using Jackson with JAXB annotations on my
>> representations.
>>
>> These annotations are used for producing and consuming JSON as well as XML.
>>
>> If a user POSTs a String to a data member (buried deep inside my
>> representation object) that is a Double, I wish to catch the resulting
>> marshal error and return a nicely formatted message.
>>
>> I want to catch *all* such marshaling errors before presenting my error
>> response back, so that there is complete feedback to the user on what they
>> need to correct in their POST.
>>
>> All I can do currently is map the resulting exception and present an error
>> for the first such encountered marshaling error.
>>
>>
>>
>> --
>> View this message in context: http://jersey.576304.n2.nabble.com/Catch-marshaling-errors-and-present-in-one-go-tp7583221.html <http://jersey.576304.n2.nabble.com/Catch-marshaling-errors-and-present-in-one-go-tp7583221.html>
>> Sent from the Jersey mailing list archive at Nabble.com <http://nabble.com/>.
>
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://jersey.576304.n2.nabble.com/Catch-marshaling-errors-and-present-in-one-go-tp7583221p7583222.html <http://jersey.576304.n2.nabble.com/Catch-marshaling-errors-and-present-in-one-go-tp7583221p7583222.html>
> To unsubscribe from Catch marshaling errors and present in one go, click here <applewebdata://B0B36651-3AD2-404A-9386-29C5A45361E6>.
> NAML <http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
> View this message in context: Re: Catch marshaling errors and present in one go <http://jersey.576304.n2.nabble.com/Catch-marshaling-errors-and-present-in-one-go-tp7583221p7583223.html>
> Sent from the Jersey mailing list archive <http://jersey.576304.n2.nabble.com/> at Nabble.com <http://nabble.com/>.