Wrong rest service is selected

101 views Asked by At

i have resource with two methods:

@GET
@Path("/{date}")
public Response getPartnerInteractionsByDate(
        @PathParam("partnerId") int partnerId,
        @PathParam("date") String date
);

@GET
@Path("/{interactionId}")
public Response getPartnerInteraction(
        @PathParam("partnerId") int partnerId,
        @PathParam("interactionId") int interactionId
);

how can I select one or another service? if I put "1" for interactionId, still the method with date gets executed. I am using apache cxf 3.0.1. Thank you

2

There are 2 answers

3
Chris On BEST ANSWER

There are two problems. The first is that your Path must contain the PathParams you're binding. Second, your two paths are identical in that they both take 2 parameters of the same type (cxf has no way of knowing the difference between an int and a string containing an int).

Try something like this:

@GET
@Path("/{partnerId}/date/{date}")
public Response getPartnerInteractionsByDate(
        @PathParam("partnerId") int partnerId,
        @PathParam("date") String date
);


@GET
@Path("/{partnerId}/{interactionId})
public Response getPartnerInteraction(
        @PathParam("partnerId") int partnerId,
        @PathParam("interactionId") int interactionId
);
1
Paul Samsotha On

The thing is, your template expressions have ambiguous paths. After the three main sorting keys [1], the path is still ambiguous. URIs don't know about types. Everything is the URI (and during URI matching) is a String. The template expression values will later get converted to our method parameter types (if it is a convertible type [2]).

That being said, a way you can disambiguate the two is by using regular expression for the template. For example

@Path("/{date: \\d{2}-\\d{2}-\\d{4}}")
public Response getPartnerInteractionsByDate

@Path("/{interactionId: \\d{10}}")
public Response getPartnerInteraction

This is assuming dates com in MM-DD-YYYY format and interaction ids are ten digits. This is just an example, but shows how you can make it work.

If there are absolutely no regex patterns that can match the URIs, then you will need to change the path (add a part to one of them) to disambiguate.


[1]

  1. The primary key of the sort is the number of literal characters in the full URI matching pattern.
  2. The secondary key of the sort is the number of template expressions embedded within the pattern
  3. The tertiary key of the sort is the number of nondefault template expressions. A default template expression is one that does not define a regular expression

[2]

  1. It is a primitive type. The int, short, float, double, byte, char, and boolean types all fit into this category.
  2. It is a Java class that has a constructor with a single String parameter.
  3. It is a Java class that has a static method named valueOf() that takes a single String argument and returns an instance of the class.
  4. It is a java.util.List<T>, java.util.Set<T>, or java.util.SortedSet<T>, where T is a type that satisfies criteria 2 or 3 or is a String.