HttpServletRequest injection on RequestScoped bean CDI
Asked Answered
C

1

4

I'm looking for a way in order to inject a @RequestScoped custom class into my @Stateless JAX-RS endpoint:

I want each time the application receives a request my custom class is injected in my JAX-RS endpoint.

Custom class:

@RequestScoped
public class CurrentTransaction {

    private String user;
    private String token;

    @PersistenceContext(name="mysql")
    protected EntityManager em;

    @Inject HttpServletRequest request;

    public CurrentTransaction() {
        this.user = request.getHeader("user");
        this.token = request.getHeader("token");
    }

    //getters and setters ...
}

So, I declare my CurrentTransaction class as @RequestScoped in order to be initialized each time a request is received. In order to do this, I need to access to HttpServletResquest in order to get header parameters.

JAX-RS endpoint:

@Stateless
@Path("/areas")
public class AreasEndpoint {

   @PersistenceContext(unitName = "mysql")
   protected EntityManager em;

   @Inject
   protected CurrentTransaction current_user_service;

    @POST
    @Path("postman")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Authentication
    public Response create(AreaRequest request) {

        if (this.current_user_service.getUser() == null) {
            System.out.println("Go!!!");
            return Response.status(Status.FORBIDDEN).build();
        } else {
            System.out.println("---- user: " + this.current_user_service.getUser());
            System.out.println("---- token: " + this.current_user_service.getToken());
        }
    }

    // ...
}

CDI arrive to perform the constructor of CurrentTransaction class. However, HttpServletRequest request field is not initialized (injected).

What am I doing wrong?

Cocker answered 5/5, 2015 at 7:47 Comment(1)
Where are you deploying to? Include versions. Any stack traces would help as well.Stretchy
D
2

A late answer on this one--maybe useful to other readers: dependency injection in CDI is done in the following order:

  1. the constructor is invoked
  2. fields are injected
  3. @PostConstruct annotated method is invoked

The last point is where you want to step in for further initialization that needs access on the injected fields:

@Inject HttpServletRequest request;

public CurrentTransaction() {
    // field injection has not yet taken place here
}

@PostConstruct
public void init() {
    // the injected request is now available
    this.user = request.getHeader("user");
    this.token = request.getHeader("token");
}
Damn answered 20/5, 2017 at 15:27 Comment(2)
Do you suggest me to "inject" fields in @PostConstruct? Could you provide me any stuff helping code?Cocker
@user3227319 Please see my edit for the needed changes in code.Damn

© 2022 - 2024 — McMap. All rights reserved.