3
votes

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

2 Answers

3
votes

@Provider implementations are discovered automatically on the server side, but not on the client side.

Just try this:

@Inject
private Somefilter someFilter;

ClientBuilder.newBuilder().register(someFilter);

Make sure to add a bean-defining annotation to Somefilter (e.g. @Dependent).

0
votes

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.