On Apr 21, 2010, at 5:02 PM, Diego Gil wrote:
> On Wed, 2010-04-21 at 16:51 +0200, Paul Sandoz wrote:
>> Hi Diego,
>>
>> Some questions:
>>
>> 1) What version of Jersey are you using?
>>
>
> 1.1.5
>
>> 2) Are you deploying as a servlet?
>
> Yes.
>
>> When you say custom format do you mean returning some HTML as
>> response
>> entity or some XML or JSON?
>>
>
> The requirement is to return error responses as XML. We handle all,
> data
> and errors as XML entities. We already do with the exceptions thrown
> in
> our code. We are trying to figure out how to catch exceptions thrown
> in
> Jersey, at the beginning of the request. F.i., when the client
> application send a String id as path parameter when we expect a Long
> id.
> In that case a 404 error (html format) is returned. We would like to
> return some XML response.
>
Yes, it is Web container returning that.
>> You can use an exception mapper for WebApplicatio, check the Response
>> from that exception and then create a new response as appropriate or
>> return the existing response.
>>
>
> There is any example ?. One of my coworkers is playing with
> ExceptionMapper, without too much luck until now.
>
If you are registering classes via scanning (e.g. packages) then make
sure the package is declared, you should get some log output that the
provider class has been registered.
@Provider
public class MyMapper implements
ExceptionMapper<WebApplicationException> {
public Response toResponse(WebApplicationException e) {
Response r = e.getResponse();
if (r.getStatus() == 405) {
return Response.fromResponse(r).entity(...).build();
}
return r;
}
}
Jersey does throw specialized exceptions for the case of parameter
matching errors. See:
https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/ParamException.html
But does not currently do so for the case of matching related to
methods and media types (405, 406 and 415).
Paul.
>> Paul.
>>
>> On Apr 21, 2010, at 4:43 PM, Diego Gil wrote:
>>
>>> Hi,
>>>
>>> We are developing a small web services repository with Jersey and
>>> facing
>>> now a little issue with error messages.
>>>
>>> If the client makes a wrong request, calling a non existing method
>>> due
>>> to a typo, gets a 405 response. That is ok but we would like to
>>> catch
>>> this exception to use a custom format.
>>>
>>> There is any example of that ?.
>>>
>>> We should use ExceptionMapper or there is another hook to catch that
>>> early errors?
>>>
>>> Thanks,
>>> Diego.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>