Reputation: 11289
I would like to have multiple functions for the same GET Path.
As well i would like my web service to "find" these functions if and only if the query parameters match the parameters in the URL String.
For example:
I have the Path("/myGET")
And for that path i would like to have 2 functions:
@GET @Produces(MediaType.APPLICATION_JSON) @TypeHint(TagSets.class) public Response getTagSets(@QueryParam("entityId") Integer entityId) { ... }
And
@GET @Produces(MediaType.APPLICATION_JSON) @TypeHint(TagSets.class) public Response getTagSets(){ ... }
Right now i am getting an error:
SEVERE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Producing media type conflict. The resource methods public javax.ws.rs.core.Response<...>.getTagSets(java.lang.Integer) and public javax.ws.rs.core.Response<...>.getTagSets(java.lang.Integer,java.lang.Integer) can produce the same media type SEVERE: Producing media type conflict. The resource methods public javax.ws.rs.core.Response<...>.getTagSets() and public javax.ws.rs.core.Response <...>.getTagSets(java.lang.Integer,java.lang.Integer) can produce the same media type
So first: Is there any way to achieve what i want to do here..
second:
If this is available, is there any way that a path will be found if and only if the query parameters match exactly what is requested in the function? for example if the same path will be called with @QueryParam("differentParam")
it will not reach any of the 2 functions.
Third: If there is no way to do this with Jersey is there a way to do it with any other frame work?.
IMPORTANT: As people that answer the question think that i am looking for a work around and not a solution. today i am using 1 function and checking the parameters and invoking what i need from this (that is what i used before i posted the question). But what i am looking for is maybe using the frameworks power to save me the trouble
Thanks.
Upvotes: 19
Views: 9053
Reputation: 7334
If there is no way to do this with Jersey is there a way to do it with any other frame work?
Spring is capable of achieving this desired mapping: https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-requestmapping.html#mvc-ann-requestmapping-params-and-headers
This can be accomplished using the params
element of the request mapping annotation. For example:
@GetMapping(value = "/tagSets", produces = MediaType.APPLICATION_JSON_VALUE, params = "entityId")
public TagSets getTagSets(@RequestParam("entityId") Integer entityId) {
...
}
@GetMapping(value = "/tagSets", produces = MediaType.APPLICATION_JSON_VALUE)
public TagSets getTagSets() {
...
}
Upvotes: 0
Reputation: 6487
A resource is uniquely defined by its path, and not by its params. Two resources you defined have the same path. You can either define new paths for each of them like /myGet/entity
, /myGet/
, /myGet/differentParam
; or use a single path as /myGet/
and check the query params as following:
@GET
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(@Context HttpServletRequest request){
...
if (request.getParameterMap().isEmpty()) {
// then you have no query params, implement as there are no query params
} else {
String queryParam = request.getQueryString();
// check queryParam, and add another if else statements, implement
}
...
}
Upvotes: 6
Reputation: 103
The only way is as described in the comments. You can use a single method which declares all query params and then based on values (exists or not), calls the correct method.
(Work around approach) If all you want is to expose a single endpoint with multiple query param to the client and code implementation doesn't matter, you can use interceptors. read the query param and modify URI to direct it to proper method.
Upvotes: 1