Multiple GET Methods: Choose the Most Specific
I have a web service that looks like this:
@Path("/ws")
public class Ws {
@GET public Record getOne(@QueryParam("id") Integer id) { return record(id); }
@GET public List<Record> getAll() { return allRecords(); }
}
The idea is that I can either call:
-
http://ws:8080/ws?id=1
to get a specific entry -
http://ws:8080/ws
to get all available records
However, when I use the second url, the first method @GET
is called with null id
.
Is there a way to achieve what I want without using different paths?
I think this can be achieved with Spring using annotations @RequestMapping(params={"id"})
and @RequestMapping
for the first and second methods respectively, but I cannot use Spring in this project.
+3
source to share
1 answer
Since the path is the same, you cannot map it to another method. If you change the path using REST style mapping
@Path("/ws")
public class Ws {
@GET @Path("/{id}") public Response getOne(@PathParam("id") Integer id) { return Response.status(200).entity(record(id)).build(); }
@GET public Response getAll() { return Response.status(200).entity(allRecords()).build(); }
then you should use:
-
http://ws:8080/ws/1
to get a specific entry -
http://ws:8080/ws
to get all available records
+2
source to share