You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Andrei Shakirin <as...@talend.com> on 2013/02/08 16:26:55 UTC

[JAXRS] Mathing resource method based on empty/non-empty body

Hi,

Use case: there is JAXRS service interface containing two POST methods:

                    @POST
                    @Path("{userId}/status")
                    Customer activate(@PathParam("userId") long userId, @QueryParam("token") String token);

                    @POST
                    @Path("{userId}/status")
                    Customer activate(@PathParam("userId") long userId, @QueryParam("token") String token,
                                                SyncPolicyTO policyTO);

Both methods have the same URL, the difference is just that the first one expects no request body and the second one expects policyTO object in the request body.
Question: is it basically possible for runtime to match resource method based on this criteria?
I didn't find explicit statement regarding it in the JAXRS spec.
Currently for CXF:

a)      In case if method has @QueryParam, I always receive either BadRequest error from the service by call activate()

b)      If method has no @QueryParam, the first method is always called
Looks a little bit strange for me.

Any ideas how it should work?

Regards,
Andrei.

RE: [JAXRS] Mathing resource method based on empty/non-empty body

Posted by Andrei Shakirin <as...@talend.com>.
Hi Sergei,

> The bottom line though, the selection is unpredictable in this case.
> 
> I think you have a couple of options:
> - use CXF ResourceComparator to customize the selection further, Jan wrote
> the implementation for selecting the methods based on a number of query
> parameters, etc...

That makes sense - thinking how to refactor these methods in best way.

Thanks a lot!

Andrei.

> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Freitag, 8. Februar 2013 16:55
> To: users@cxf.apache.org
> Subject: Re: [JAXRS] Mathing resource method based on empty/non-empty
> body
> 
> Hi,
> On 08/02/13 15:26, Andrei Shakirin wrote:
> > Hi,
> >
> > Use case: there is JAXRS service interface containing two POST methods:
> >
> >                      @POST
> >                      @Path("{userId}/status")
> >                      Customer activate(@PathParam("userId") long
> > userId, @QueryParam("token") String token);
> >
> >                      @POST
> >                      @Path("{userId}/status")
> >                      Customer activate(@PathParam("userId") long userId,
> @QueryParam("token") String token,
> >                                                  SyncPolicyTO
> > policyTO);
> >
> > Both methods have the same URL, the difference is just that the first one
> expects no request body and the second one expects policyTO object in the
> request body.
> > Question: is it basically possible for runtime to match resource method
> based on this criteria?
> > I didn't find explicit statement regarding it in the JAXRS spec.
> 
> JAX-RS selection algorithm does not take the method parameters into
> consideration, so the selection is random
> 
> > Currently for CXF:
> >
> > a)      In case if method has @QueryParam, I always receive either
> BadRequest error from the service by call activate()
> >
> 
> I guess when you have @QueryParam (the extra Java method parameter),
> JRE will list the method with SyncPolicyTo first and hence JAX-RS runtime
> selects it first, and then, assuming SyncPolicyTO is JAXB bean, it is the job of
> JAXB provider to populate it and it will throw 400 if the actual message body
> from which SyncPolicyTO is to be populated is empty, this 400 is per the spec
> 
> > b)      If method has no @QueryParam, the first method is always called
> > Looks a little bit strange for me.
> >
> I guess in this case the runtime lists the former method first.
> 
> The bottom line though, the selection is unpredictable in this case.
> 
> I think you have a couple of options:
> - use CXF ResourceComparator to customize the selection further, Jan wrote
> the implementation for selecting the methods based on a number of query
> parameters, etc...
> - use only a single method,
> 
> Customer activate(@PathParam("userId") long userId, QueryParam("token")
> String token, SyncPolicyTO policyTO);
> 
> and add org.apache.cxf.jaxrs.ext.Nullable annotation to SyncPolicyTO, if the
> payload is not empty - then SyncPolicyTO will be populated, otherwise, if
> @Nullable is there, null will be injected
> 
> HTH, Sergey
> 
> > Any ideas how it should work?
> >
> > Regards,
> > Andrei.
> >
> 


Re: [JAXRS] Mathing resource method based on empty/non-empty body

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,
On 08/02/13 15:26, Andrei Shakirin wrote:
> Hi,
>
> Use case: there is JAXRS service interface containing two POST methods:
>
>                      @POST
>                      @Path("{userId}/status")
>                      Customer activate(@PathParam("userId") long userId, @QueryParam("token") String token);
>
>                      @POST
>                      @Path("{userId}/status")
>                      Customer activate(@PathParam("userId") long userId, @QueryParam("token") String token,
>                                                  SyncPolicyTO policyTO);
>
> Both methods have the same URL, the difference is just that the first one expects no request body and the second one expects policyTO object in the request body.
> Question: is it basically possible for runtime to match resource method based on this criteria?
> I didn't find explicit statement regarding it in the JAXRS spec.

JAX-RS selection algorithm does not take the method parameters into 
consideration, so the selection is random

> Currently for CXF:
>
> a)      In case if method has @QueryParam, I always receive either BadRequest error from the service by call activate()
>

I guess when you have @QueryParam (the extra Java method parameter), JRE 
will list the method with SyncPolicyTo first and hence JAX-RS runtime 
selects it first, and then, assuming SyncPolicyTO is JAXB bean, it is 
the job of JAXB provider to populate it and it will throw 400 if the 
actual message body from which SyncPolicyTO is to be populated is empty, 
this 400 is per the spec

> b)      If method has no @QueryParam, the first method is always called
> Looks a little bit strange for me.
>
I guess in this case the runtime lists the former method first.

The bottom line though, the selection is unpredictable in this case.

I think you have a couple of options:
- use CXF ResourceComparator to customize the selection further, Jan 
wrote the implementation for selecting the methods based on a number of 
query parameters, etc...
- use only a single method,

Customer activate(@PathParam("userId") long userId, QueryParam("token") 
String token, SyncPolicyTO policyTO);

and add org.apache.cxf.jaxrs.ext.Nullable annotation to SyncPolicyTO, if 
the payload is not empty - then SyncPolicyTO will be populated, 
otherwise, if @Nullable is there, null will be injected

HTH, Sergey

> Any ideas how it should work?
>
> Regards,
> Andrei.
>