AbstractContainerRequestValueFactory removed from Jersey 2.26

Viewed 1069

I upgrade Jersey in my project to 2.26 version.

My code is:

    import org.glassfish.jersey.server.internal.inject.AbstractContainerRequestValueFactory;

    public class ClassA extends AbstractContainerRequestValueFactory<ClassB> {

            @Override
            public ClassB provide() {
                    .....
            }

    }

AbstractContainerRequestValueFactory class was removed, and I didn't found how to fix this.

2 Answers

Alternatively, we can use custom HK2 bindings, that are configured as part of Jersey application. Add jersey-hk2 dependency dependency in the classpath org.glassfish.jersey.inject:jersey-hk2

Define the Factory class to generate the instance based on the resource scopes

import org.glassfish.hk2.api.Factory;
import javax.ws.rs.ext.Provider;
@Provider
public class ClassA implements Factory<ClassB> {
    @Override
    public ClassB provide() {
        // construct ClassB instance based on your requirement 
        //here I am simply returning the object
        return new ClassB();
    }
    @Override
    public void dispose(ClassB instance) {/**Noop**/}
}

Registering the custom factory class

For instance, I have to inject ClassB instance for every request then I can register the above factory with the scope of RequestScoped, in such case, for every request ClassA#provide will be called to create the value of ClassB instance that can be retrieved as @Context ClassB classB

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ext.Provider;
@Provider
class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bindFactory(ClassA.class)
                        .to(ClassB.class)
                        .in(RequestScoped.class);
            }
        });
    }
}

The following scopes are currently supported by Jersey enter image description here

Related