Dave
Dave

Reputation: 1587

How to Inject RequestParameters using Guice

I am trying to get the RequestParamters to be injected into a Guice-powered class I'm using as a servlet. I'm running it with Jersey & embedded Jetty. I declare the class like this:

@Path("/my_url")
@RequestScoped
public class MyUrlServlet {

// try to inject the RequestParameters
@Inject
@RequestParameters
private final Map<String, String[]> reqParms;
...
}

And I get errors at runtime stating that:

1) No implementation for java.util.Map was bound.
  while locating java.util.Map
    for parameter 1 at com.nurloc.rest.account.LoginServlet.(MyUrlServlet.java:31)
  at com.nurloc.startup.ServletMappingConfig$1.configureServlets(ServletMappingConfig.java:34)

The docs also state that if you use this in a singleton class you need to use a provider, which I'm not certain how to write. Is it a provider that provides a Map<String, String[]>? If so, I didn't have any luck there either.

Upvotes: 2

Views: 1354

Answers (1)

ColinD
ColinD

Reputation: 110054

You don't show how your "servlet" class is being bound or used, but it looks to me like it's being created outside of a request for whatever reason. If that's the case, you won't be able to inject the parameters directly. You should be able to inject a Provider instead, like this:

@Inject @RequestParameters
private Provider<Map<String, String[]>> reqParamsProvider;

You then have to make sure you only call get() on the provider when you're in a request.

Upvotes: 2

Related Questions