GithubHelp home page GithubHelp logo

Comments (5)

heldeen avatar heldeen commented on August 26, 2024 1

Deleting that code breaks the binding for HK2 to Guice. You can get the info you need from the javax.ws.rs.container.ContainerRequestContext that is provided in the javax.ws.rs.container.ContainerRequestFilter#filter(ContainerRequestContext) like so:

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.getUriInfo().getBaseUri().getPort();
        requestContext.getUriInfo().getBaseUri().getPath();
    }

The reason it is failing is Jersey is initializing the filter at start time and there is no HTTP Request at that time.

from dropwizard-guice.

heldeen avatar heldeen commented on August 26, 2024 1

It has to do w/ how Guice, Guice Servlet, and Jersey2-Guice works vs just plain Jersey/HK2. When the JerseyGuiceUtils.install code is in place the org.glassfish.hk2.api.ServiceLocator that Jersey uses has a preference to satisfy injections from the Guice object graph before the HK2 locator is consulted. The Guice com.google.inject.servlet.ServletModule sets up a provider for javax.servlet.http.HttpServletRequest that is com.google.inject.servlet.RequestScoped and that is why it fails to provide the request object at Jersey startup time; there is no request happening then.

On the other hand if just Jersey/HK2 are involved than the javax.ws.rs.core.@Context annotated javax.servlet.http.HttpServletRequest that HK2 provides for injection is a proxy for a thread local pointer to the current request. The proxy is setup and injected at Jersey startup. As long as that proxy object is only accessed during a request there is no problem.

There are lots of ways to work around it. My earlier suggestion of just using the javax.ws.rs.container.ContainerRequestContext that is provided in the javax.ws.rs.container.ContainerRequestFilter#filter(ContainerRequestContext) is probably the best. The Guice way to get the same functionality as Jersey/HK would be to inject a javax.inject.Provider<javax.servlet.http.HttpServletRequest> instead of the barejavax.servlet.http.HttpServletRequest. Guice can inject a Provider at Jersey startup and as long as the the provider is only asked for the current request during a request there would be no errors.

I would also suggest not mixing JAX-RS and Servlet specs in your filter. Only use the JAX-RS core inside Jersey and only use Servlet inside Jetty.

Hope this helps!

from dropwizard-guice.

itgo067 avatar itgo067 commented on August 26, 2024

I find some more infomations for this issue, when i remove this code , working well!

JerseyGuiceUtils.install(new ServiceLocatorGenerator() {
            @Override
            public ServiceLocator create(String name, ServiceLocator parent) {
                if (!name.startsWith("__HK2_Generated_")) {
                    return null;
                }

                return injector.createChildInjector(new JerseyGuiceModule(name)).getInstance(ServiceLocator.class);
            }
        });

from dropwizard-guice.

itgo067 avatar itgo067 commented on August 26, 2024

I got it.
But just like me said , why I delete JerseyGuiceUtils.install code. I can use javax.servlet.HttpServletRequest, It will be work fine. can you explain it ? Thanks !

from dropwizard-guice.

jhaber avatar jhaber commented on August 26, 2024

Thanks for the explanation and the good advice

from dropwizard-guice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.