How to inject CDI dependency into JAX-RS ClientRequestFilter provider

Viewed 2505

I have a @Provider that implements a ClientRequestFilter. The provider itself is not discovered at all. When I register it manually, then it is discovered.

ClientBuilder.newBuilder().register(Somefilter.class);

This way however, CDI doesn't inject dependencies into Somefilter.

  • AS: WildFly 10
  • JAX-RS : Resteasy (built in WildFly)

The code of my Somefilter class looks like this.

@Provider
public class Somefilter implements ClientRequestFilter {

@Inject
private AccountService accountService;


@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    System.out.println(accountService);

}

}

The application also has a server part, configured with @Application annotation, using some classes implementing ContainerRequestFilter. These are discovered by Resteasy and even CDI injection works.

So the question is, how can I make client-api related providers inject CDI dependencies.

2 Answers

Make sure you are importing the correct package javax.enterprise.context for declaring the @RequestScoped scope for your service class:

import javax.enterprise.context.RequestScoped;

@Named
@RequestScoped
public class AccountService {

}

I was having this problem and found out I was erroneously using the Faces (JSF) package instead which was javax.faces.bean.RequestScoped.

Related