You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Raul Acevedo <ra...@cantara.com> on 2014/04/09 21:05:01 UTC
customize error messages for bad input requests
Hello,
I'm using CXF 2.7.5 for some REST APIs, most of which return JSON. I'd
like to return a nice custom error message that tells the user calling
the API what fields failed when the user provides bad input; e.g.
passing in a string for an Integer parameter. Essentially I'd like to
intercept input error handling (either by type conversion or bean
validation) and tell the user: "here are the input fields that I didn't
understand", rather than a blank page with a 404.
The whole point is to avoid having API arguments all be strings that we
have to manually convert to their native types, catch exception, and
creating the nice error message. I.e. I don't want to do:
@GET @PATH("/{foo}/{bar}")
public Response foo(@PathParam("foo") Integer foo,
@PathParam("bar") Integer bar) {
List<String> failedInputNames = new ArrayList<String>();
if (cannotParseInt(foo)) failedInputNames.add("foo");
if (cannotParseInt(bar)) failedInputNames.add("bar");
if (!failedInputNames.isEmpty() {
return Response.status(NOT_FOUND, "there was an input error
with these fields: " + failedInputNames);
}
... do stuff ...
}
I would like to automate this so it automagically happens for all our
API endpoints; if any inputs cannot be bound, or fail bean validation
(e.g. @Pattern or @NotNull), the API endpoint code remains simple and
clean:
@GET @PATH("/{foo}/{bar}")
public Response foo(@PathParam("foo") Integer foo,
@PathParam("bar") Integer bar) {
... do stuff ...
}
In Spring this is possible by adding a BindingResult argument to the
method; if it's present, Spring doesn't throw an exception, but tells
you via BindingResult which input fields failed, and why. This makes it
very easy to write custom code to tell the user in a friendly way what
input failed.
Is something like this possible via CXF? It's not obvious to me that
CXF will generate a consistent Exception type I can intercept.
Thanks,
Raul
Re: customize error messages for bad input requests
Posted by Sergey Beryozkin <sb...@gmail.com>.
On 10/04/14 22:23, Raul Acevedo wrote:
> On 2014-04-10 12:49, Jose María Zaragoza wrote:
>
>> According http://cxf.apache.org/docs/validationfeature.html. exception
>> should be
>> javax.validation.ConstraintViolationException or
>> javax.validation.ValidationException
>>
>> How are you setting up Bean Validation feature ?
>
> I'm still trying to set it up, but that's not the problem. I'm still
> trying to customize the response when CXF converts arguments to the
> native Java types; this isn't done by bean validation. Per my original
> example:
>
> @GET @PATH("/{foo}/{bar}")
> public Response foo(@PathParam("foo") Integer foo,
> @PathParam("bar") Integer bar) {
> ... do stuff ...
> }
>
> No bean validation is used here, this is just basic input parameter
> binding. CXF throws a fairly generic ClientErrorException, which
> doesn't tell me that the problem is that input binding failed.
You are right, Bean validation can not be effective here given that it
is applied after the parameters have been initialized, I missed it...
Note ClientErrorException will have a cause initialized to
NumberFormatException in this case, so this is one way to manage it.
Otherwise I can only think of registering a PreMatch
ContainerRequestFilter which will validate the request URI.
HTH, Sergey
>
> Ideally I'd like a solution that works for both bean validation (once I
> get that working) and input binding, but for now I don't see a way to do
> even the basic input binding error response handler.
>
> Thanks,
>
> Raul
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: customize error messages for bad input requests
Posted by Jose María Zaragoza <de...@gmail.com>.
2014-04-10 23:23 GMT+02:00 Raul Acevedo <ra...@cantara.com>:
> On 2014-04-10 12:49, Jose María Zaragoza wrote:
>
>> According http://cxf.apache.org/docs/validationfeature.html. exception
>> should be
>> javax.validation.ConstraintViolationException or
>> javax.validation.ValidationException
>>
>> How are you setting up Bean Validation feature ?
>
>
> I'm still trying to set it up, but that's not the problem. I'm still trying
> to customize the response when CXF converts arguments to the native Java
> types; this isn't done by bean validation. Per my original example:
>
OK. I understand
I'm reading
http://cxf.apache.org/docs/jax-rs-basics.html , in "Dealing with Parameters"
I don't know , but maybe you need to wrapper the parameters into a BeanParam
https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/BeanParam.html
But I guess you will need to declare parameters as String and apply
validation to them ( number format )
But I'm not sure
Maybe , in this case, bean validation works
> @GET @PATH("/{foo}/{bar}")
> public Response foo(@PathParam("foo") Integer foo, @PathParam("bar")
> Integer bar) {
> ... do stuff ...
> }
>
> No bean validation is used here, this is just basic input parameter binding.
> CXF throws a fairly generic ClientErrorException, which doesn't tell me that
> the problem is that input binding failed.
>
> Ideally I'd like a solution that works for both bean validation (once I get
> that working) and input binding, but for now I don't see a way to do even
> the basic input binding error response handler.
>
> Thanks,
>
> Raul
Re: customize error messages for bad input requests
Posted by Raul Acevedo <ra...@cantara.com>.
On 2014-04-10 12:49, Jose María Zaragoza wrote:
> According http://cxf.apache.org/docs/validationfeature.html. exception
> should be
> javax.validation.ConstraintViolationException or
> javax.validation.ValidationException
>
> How are you setting up Bean Validation feature ?
I'm still trying to set it up, but that's not the problem. I'm still
trying to customize the response when CXF converts arguments to the
native Java types; this isn't done by bean validation. Per my original
example:
@GET @PATH("/{foo}/{bar}")
public Response foo(@PathParam("foo") Integer foo,
@PathParam("bar") Integer bar) {
... do stuff ...
}
No bean validation is used here, this is just basic input parameter
binding. CXF throws a fairly generic ClientErrorException, which
doesn't tell me that the problem is that input binding failed.
Ideally I'd like a solution that works for both bean validation (once I
get that working) and input binding, but for now I don't see a way to do
even the basic input binding error response handler.
Thanks,
Raul
Re: customize error messages for bad input requests
Posted by Jose María Zaragoza <de...@gmail.com>.
2014-04-10 20:55 GMT+02:00 Raul Acevedo <ra...@cantara.com>:
> On 2014-04-09 21:35, Sergey Beryozkin wrote:
>
>> Can you try and manage it at JAX-RS Bean Validation level,
>> specifically, by adding a ValidationException mapper or at the very
>> least a RuntimeException mapper ?
>
>
> CXF doesn't throw ValidationException when it tries to bind a string to an
> integer; it throws ClientErrorException, so creating
> ExceptionHandler<ClientErrorException> works, except that exception is too
> general, it doesn't tell me that the error was trying to bind input
> arguments, or what input arguments failed. It's documented as "A base
> runtime application exception indicating a client request error (HTTP {@code
> 4xx} status codes)."
>
> So in theory ClientErrorException can happen for a wide variety of reasons;
> I can't use it determine if there was an input error.
>
> Is there some other way of customizing the response when CXF can't bind
> input arguments?
According http://cxf.apache.org/docs/validationfeature.html. exception should be
javax.validation.ConstraintViolationException or
javax.validation.ValidationException
How are you setting up Bean Validation feature ?
>
> Thanks,
>
> Raul
Re: customize error messages for bad input requests
Posted by Raul Acevedo <ra...@cantara.com>.
On 2014-04-09 21:35, Sergey Beryozkin wrote:
> Can you try and manage it at JAX-RS Bean Validation level,
> specifically, by adding a ValidationException mapper or at the very
> least a RuntimeException mapper ?
CXF doesn't throw ValidationException when it tries to bind a string to
an integer; it throws ClientErrorException, so creating
ExceptionHandler<ClientErrorException> works, except that exception is
too general, it doesn't tell me that the error was trying to bind input
arguments, or what input arguments failed. It's documented as "A base
runtime application exception indicating a client request error (HTTP
{@code 4xx} status codes)."
So in theory ClientErrorException can happen for a wide variety of
reasons; I can't use it determine if there was an input error.
Is there some other way of customizing the response when CXF can't bind
input arguments?
Thanks,
Raul
Re: customize error messages for bad input requests
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
Can you try and manage it at JAX-RS Bean Validation level, specifically,
by adding a ValidationException mapper or at the very least a
RuntimeException mapper ?
Cheers, Sergey
On 09/04/14 20:05, Raul Acevedo wrote:
> Hello,
>
> I'm using CXF 2.7.5 for some REST APIs, most of which return JSON. I'd
> like to return a nice custom error message that tells the user calling
> the API what fields failed when the user provides bad input; e.g.
> passing in a string for an Integer parameter. Essentially I'd like to
> intercept input error handling (either by type conversion or bean
> validation) and tell the user: "here are the input fields that I didn't
> understand", rather than a blank page with a 404.
>
> The whole point is to avoid having API arguments all be strings that we
> have to manually convert to their native types, catch exception, and
> creating the nice error message. I.e. I don't want to do:
>
> @GET @PATH("/{foo}/{bar}")
> public Response foo(@PathParam("foo") Integer foo,
> @PathParam("bar") Integer bar) {
> List<String> failedInputNames = new ArrayList<String>();
> if (cannotParseInt(foo)) failedInputNames.add("foo");
> if (cannotParseInt(bar)) failedInputNames.add("bar");
> if (!failedInputNames.isEmpty() {
> return Response.status(NOT_FOUND, "there was an input error
> with these fields: " + failedInputNames);
> }
> ... do stuff ...
> }
>
> I would like to automate this so it automagically happens for all our
> API endpoints; if any inputs cannot be bound, or fail bean validation
> (e.g. @Pattern or @NotNull), the API endpoint code remains simple and
> clean:
>
>
> @GET @PATH("/{foo}/{bar}")
> public Response foo(@PathParam("foo") Integer foo,
> @PathParam("bar") Integer bar) {
> ... do stuff ...
> }
>
> In Spring this is possible by adding a BindingResult argument to the
> method; if it's present, Spring doesn't throw an exception, but tells
> you via BindingResult which input fields failed, and why. This makes it
> very easy to write custom code to tell the user in a friendly way what
> input failed.
>
> Is something like this possible via CXF? It's not obvious to me that CXF
> will generate a consistent Exception type I can intercept.
>
> Thanks,
>
> Raul
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
RE: customize error messages for bad input requests
Posted by Raul Acevedo <ra...@cantara.com>.
On 2014-04-09 18:57, XiLai Dai wrote:
> In case bad parameter value, the exception (e.g.
> IllegalParameterException) should be thrown, then the
> XXXExceptionMapper will create Response accordingly.
There is no IllegalParameterException in CXF that I can find. Can you
tell me the full package and class name that CXF throws when there's an
error binding input arguments?
Thanks,
Raul
RE: customize error messages for bad input requests
Posted by XiLai Dai <xl...@talend.com>.
Hi,
Just for your information. We create XXXExceptionMapper class which implements javax.ws.rs.ext.ExceptionMapper and add it into <jaxrs:providers>, for example,
<jaxrs:server id="registryService" address="/registry/lookup">
<jaxrs:serviceBeans>
<ref component-id="registryLookupService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.talend.esb.registry.service.lookup.RegistryExceptionMapper" />
</jaxrs:providers>
</jaxrs:server>
In case bad parameter value, the exception (e.g. IllegalParameterException) should be thrown, then the XXXExceptionMapper will create Response accordingly.
Regards.
Xilai
-----Original Message-----
From: Raul Acevedo [mailto:raul@cantara.com]
Sent: Thursday, April 10, 2014 3:05 AM
To: users@cxf.apache.org
Subject: customize error messages for bad input requests
Hello,
I'm using CXF 2.7.5 for some REST APIs, most of which return JSON. I'd like to return a nice custom error message that tells the user calling the API what fields failed when the user provides bad input; e.g.
passing in a string for an Integer parameter. Essentially I'd like to intercept input error handling (either by type conversion or bean
validation) and tell the user: "here are the input fields that I didn't understand", rather than a blank page with a 404.
The whole point is to avoid having API arguments all be strings that we have to manually convert to their native types, catch exception, and creating the nice error message. I.e. I don't want to do:
@GET @PATH("/{foo}/{bar}")
public Response foo(@PathParam("foo") Integer foo,
@PathParam("bar") Integer bar) {
List<String> failedInputNames = new ArrayList<String>();
if (cannotParseInt(foo)) failedInputNames.add("foo");
if (cannotParseInt(bar)) failedInputNames.add("bar");
if (!failedInputNames.isEmpty() {
return Response.status(NOT_FOUND, "there was an input error with these fields: " + failedInputNames);
}
... do stuff ...
}
I would like to automate this so it automagically happens for all our API endpoints; if any inputs cannot be bound, or fail bean validation (e.g. @Pattern or @NotNull), the API endpoint code remains simple and
clean:
@GET @PATH("/{foo}/{bar}")
public Response foo(@PathParam("foo") Integer foo,
@PathParam("bar") Integer bar) {
... do stuff ...
}
In Spring this is possible by adding a BindingResult argument to the method; if it's present, Spring doesn't throw an exception, but tells you via BindingResult which input fields failed, and why. This makes it very easy to write custom code to tell the user in a friendly way what input failed.
Is something like this possible via CXF? It's not obvious to me that CXF will generate a consistent Exception type I can intercept.
Thanks,
Raul