简体   繁体   中英

Can RestEasy choose method based on query params?

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM