I've followed the example for seeding a value in request scope (shown
below) but I cannot get it to work. I get the following error
1) Could not find a suitable constructor in java.lang.Integer. Classes must
have either one (and only one) constructor annotated with @Inject or a
zero-argument constructor that is not private.
! at java.lang.Integer.class(Integer.java:52)
! while locating com.google.inject.Provider<java.lang.Integer>
! for parameter 1 at
com.testproject.registrationservice.RegistrationResource.<init>(RegistrationResource.java:42)
The only difference with my implementation is that I am injecting the
"Provider<Integer> userIdProvider" into a jax.rs Resource but that should
not change anything since the resource is bound in singleton scope as is
the servlet shown in the example below. I have debugged and can see that
the filter is being applied, but once I add injected the provider
constructor parameter to my resource the app will fail on startup when it
tries to inject the resource.
Can someone show me what I am missing?
@Path("/")
public class RegistrationResource {
private final Provider<Integer> userIdProvider;
@Inject
public RegistrationResource(Provider<Integer> userIdProvider){
this.userIdProvider = userIdProvider;
}
......
}
"
The Filter to do this might look like this:
protected Filter createUserIdScopingFilter() {
return new Filter() {
@Override public void doFilter(
ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
// ...you'd probably want more sanity checking here
Integer userId = Integer.valueOf(httpRequest.getParameter("user-id"));
httpRequest.setAttribute(
Key.get(Integer.class, Names.named("user-id")).toString(),
userId);
chain.doFilter(request, response);
}
@Override public void init(FilterConfig filterConfig) throws
ServletException { }
@Override public void destroy() { }
};
}
And the binding might look like this:
public class YourServletModule extends ServletModule {
@Override protected void configureServlets() {
.....
filter("/process-user*").through(createUserIdScopingFilter());
}
}
And the servlet to use this might look like this:
@Singletonclass UserProcessingServlet extends HttpServlet {
private final Provider<Integer> userIdProvider;
....
@Inject UserProcessingServlet(
Provider<Integer> userIdProvider,
.....) {
this.userIdProvider = userIdProvider;
}
....
@Override public void doGet(
HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
...
Integer userId = userIdProvider.get();
}}
"
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.