5

I've started working with RestEasy and I've come across a problem that I can't seem to find an answer to. If I have 2 methods that both resolve to the same path (in this case /path1/path2/path3), but they both have a different number of query parameters, will RestEasy be able to determine which method to use?

@GET
@NoCache
@Produces({
    MediaType.APPLICATION_JSON
})
@Path("/path1/path2/{path3}")
public String getResults1(
        @PathParam("path3") String path3,
        @QueryParam("query1") @DefaultValue("") String query1,
        @QueryParam("query2") String query2,
        @QueryParam("query3") @DefaultValue("25") int query3) {
    ...
}

@GET
@NoCache
@Produces({
    MediaType.APPLICATION_JSON
})
@Path("/path1/path2/{path3}")
public String getResults2(
        @PathParam("path3") String path3,
        @QueryParam("query1") @DefaultValue("") String query1,
        @QueryParam("query2") @DefaultValue("5") Integer query2) {
    ...
}

I've done some testing and yesterday it seemed that everything was working perfectly and that it could choose the right path, but now today I'm starting to see it take the wrong path each time.

Is this something that should be handled, or should I just suck it up and put it in 1 method and do the check myself?

2
  • 3
    I love getting down votes with no explanation.
    – Shaded
    Jun 19, 2013 at 17:01
  • Not sure why you are being downvoted. +1 for a good question that I have seen come up before. Jun 19, 2013 at 17:09

1 Answer 1

2

No, you should be handling this in the method. If conflicting resources are found it is implementation independent which method will be matched.

Take a look at your example again:

If you submitted query1 and query2 how would it know if you wanted the method with 2 query parameters or the method with 3 query parameters and you wanted it to default the 3rd to it's default value?

5
  • Hm.. just seems to me that if it was matching on the query parameter name it could make an intelligent decision. Much like Java Overloading if I have a method with 2 string parameters and a method with 3 string parameters that are named the same when it's called with 2 strings it shouldn't default to use the 3 parameter method with null as the third.
    – Shaded
    Jun 19, 2013 at 17:08
  • 1
    @Shaded - Take a look at query2. How would it know if the param is an integer or a string? It could try and parse the int, but what do you do if it doesn't parse? Is that a string argument then or is it an invalid integer argument? Jun 19, 2013 at 17:11
  • You run into the same problem with one method though, if you try and pass query2=x in the url and you map query2 to an int you'll have problems. I'm looking at it more as if resteasy is looking at the params and has the annotations it could do matching to check if it has all the params for a method and use the highest match. I think spring has something like this built in. If I can find it I'll link it.
    – Shaded
    Jun 19, 2013 at 19:08
  • If you look here it's another answer that I found that deals with the same problem in spring and it has the ability to handle it. Also this spring reference has information on it in the Advanced @RequestMapping section. Just strange that RestEasy doesn't explicitly say yes or no on the subject.
    – Shaded
    Jun 19, 2013 at 19:58
  • @Shaded were you able to find a solution to this? I am looking to override the method on the basis of a flag in the parameters. I also want to use my well-structured bean validations, which also defines json properties for the overloaded methods. I do not want to use another endpoint at any cost! Jul 11, 2020 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.