0

I am currently trying to add a new feature to my REST API.

Basically I want to add the ability to add query parameters to the end of a path and turn this into a Map of all the query options for example.

my current code allows me to do things like

localhost:8181/cxf/check/
localhost:8181/cxf/check/format
localhost:8181/cxf/check/a/b
localhost:8181/cxf/check/format/a/b

and this will use all the @pathparam as String variables to generate a response.

What I want to do now is add:

localhost:8181/cxf/check/a/b/?x=abc&y=def&z=ghi&...
localhost:8181/cxf/check/format/a/b/?x=abc&y=def&z=ghi&...

and I would then have this generate a Map which can be used along with the pathparam to build the response

x => abc
y => def
z => ghi
... => ...

I was thinking something like this [Below] however the @QueryParam seem to only handle one key value and not a Map of them.

@GET
@Path("/{format}/{part1}/{part2}/{query}")
Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2, @QueryParam("query") Map<K,V> query);

below is my current interface code.

@Produces(MediaType.APPLICATION_JSON)
public interface RestService {

@GET
@Path("/")
Response getCheck();

@GET
@Path("/{format}")
Response getCheck(@PathParam("format") String format);

@GET
@Path("/{part1}/{part2}")
Response getCheck(@PathParam("part1") String part1,@PathParam("part2") String part2);

@GET
@Path("/{format}/{part1}/{part2}")
Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2);

}

1 Answers1

1

QueryParam("") myBean allows to get all the query parameters injected. Remove also the last {query} part

@GET
@Path("/{format}/{part1}/{part2}/")
Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2, @QueryParam("") MyBean myBean);

 public class MyBean{
    public void setX(String x) {...}
    public void setY(String y) {...}  
 }

You can also not declare parameters and parse the URI. This option could be useful if you can accept non-fixed parameters

 @GET
 @Path("/{format}/{part1}/{part2}/")
 public Response getCheck(@PathParam("format") String format, @PathParam("part1") String part1, @PathParam("part2") String part2, @Context UriInfo uriInfo) {
    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
    String x= params.getFirst("x");
    String y= params.getFirst("y");
}
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • The @Context is something I was looking at which I like (as it build a Map) however I was having problem getting this to work (might just need to look at a few more examples.) does this allow me to use @Path("/{format}/{part1}/{part2}/") or else do I have to leave it empty and then extract the path later on from the UriInfo? – Alec George Doran-Twyford Dec 12 '16 at 12:46
  • You can use `@PathParam` and `@Context UriInfo` together. I have included the full example in the answer – pedrofb Dec 12 '16 at 13:09
  • Thanks I will test out as soon as I can Thank you! – Alec George Doran-Twyford Dec 12 '16 at 13:10