There are a few ways to clean it up.
Note: this is only for DW [8.0, )
You could...
Just inject UriInfo and get a MultivaluedMap<String, String>. (Though you can't get MultivaluedMap<String, Bindings>)
@GET
public Response get(@Context UriInfo uriInfo) {
MultivaluedMap<String, String> queryMap = uriInfo.getQueryParameters();
}
You could...
Use @BeanParam to just put everything into a bean.
public class BindingsBean {
@QueryParam("bindings.organisation")
Bindings orgBindings;
@QueryParam("bindings.person")
Bindings personBindings;
}
@GET
public Response get(@BeanParam BindingsBean bindingsBean) {}
You could...
Create a map-like bean class and create a Factory[1] so you can inject it with @Context. For example
Map wrapper
public class BindingsMap {
public static final String BINDINGS_ORGANIZATION = "bindings.organisation";
public static final String BINDINGS_PERSON = "bindings.person";
private final Map<String, Bindings> bindings = new HashMap<>();
public void put(String key, Bindings bindings) {
this.bindings.put(key, bindings);
}
public Bindings get(String key) {
return this.bindings.get(key);
}
}
Factory
public class QueryBindingsFactory
extends AbstractContainerRequestValueFactory<BindingsMap> {
@Override
public BindingsMap provide() {
BindingsMap bindingsMap = new BindingsMap();
put(bindingsMap, BindingsMap.BINDINGS_ORGANIZATION);
put(bindingsMap, BindingsMap.BINDINGS_PERSON);
return bindingsMap;
}
private void put(BindingsMap bindingsMap, String key) {
ContainerRequest request = getContainerRequest();
MultivaluedMap<String, String> queryParams
= request.getUriInfo().getQueryParameters();
bindingsMap.put(key, Bindings.fromString(queryParams.getFirst(key)));
}
@Override
public void dispose(BindingsMap t) {
}
}
Resource method
@GET
public Response get(@Context BindingsMap bindingsMap) {}
Then you need to register the factory
env.jersey().register(new AbstractBinder(){
@Override
public void configure() {
bindFactory(QueryBindingsFactory.class)
.to(BindingsMap.class)
.in(RequestScoped.class);
}
});
[1] See Custom Injection and Lifecycle Management
You could...
Create a custom annotation to inject the above BindingsMap, if you don't like the @Context annotation. You can see a complete example in this answer. It pretty much just builds on top of the above option. It requires extra:
- A custom annotation
- An
InjectionResolver.
- And an
AbstractValueFactoryProvider
All examples can be seen in the link. If you're ok with the @Context annotation, it may be simpler just to go with that.