1

When I created my subresource, my Injection (authenticatedUser) is null. But in the root class (authenticatedUser) the Injection had the correct values. Therefore, when I used

/medicos/me

I get the correct values, but when I used

/medicos/me/pacientes

I get a error null, from authenticatedUser in subresource PacienteResource. I don't understand and how I can resolve this dependency in sub resource. This dependency (authenticatedUser) is produced in authentification filter.

@Path("/medicos")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MedicoResource {

    @Inject
    @AuthenticatedUser
    Usuario authenticatedUser;

    @GET
    @Path("me")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getMedico() {
        return Response.ok(authenticatedUser.getMedico().getNombre()).build();
    }

    @Path("/me/pacientes")
    public PacienteResource getMedicoPacientes() {
        System.out.println("get medico pacientes");
        return new PacienteResource();//This is the problem.
    }
}

Subresource class also had the authenticatedUser, but when I used it, this is null.

public class PacienteResource {

    @Inject
    @AuthenticatedUser
    private Usuario authenticatedUser;

    @Inject
    PacienteService pacienteService;

    @GET
    @RolesAllowed({"MEDICO"})
    public Set<Paciente> getPacientes() {
        Set<Paciente> pacientes = authenticatedUser.getMedico().getPacienteSet();
        return pacientes;
    }
}

This is my producer of @AuthenticatedUser. The filter "Authentification"throw a event, for this Observer "handleAuthenticationEvent". Then when I need a authenticatedUser I only should inject a @AuthenticatedUser. But when I created the new instance of pacienteResource, the AuthenticatedUser is not produced.

@RequestScoped
public class AuthenticatedUserProducer {   

    @Inject
    private UsuarioService usuarioService;

    private Usuario authenticatedUser;

    public void handleAuthenticationEvent(@Observes @AuthenticatedUser String username) {
        this.authenticatedUser = findUser(username);
    }

    private Usuario findUser(String username) {
        Usuario usuario = usuarioService.findByUserName(username);
        return usuario;
    }

    @Produces
    @RequestScoped
    @AuthenticatedUser
    public Usuario createLogger() {
        return authenticatedUser;
    }
}

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER})
public @interface AuthenticatedUser {

}
Brayme Guaman
  • 175
  • 2
  • 12
  • Q: Any chance you can reproduce the problem with an [MCVE](https://stackoverflow.com/help/mcve)? Q: What's your development/runtime environment (e.g. Eclipse? Tomcat? Jersey? Spring Boot?) – paulsm4 Nov 27 '18 at 20:16
  • JAX-RS and my server is WildFly – Brayme Guaman Nov 27 '18 at 20:24
  • Interesting - I didn't know JBoss had a new home (Redhat, now IBM) and a new name (WilldFly). SUGGESTION: See if this helps: https://stackoverflow.com/questions/40225429/cdi-weld-se-not-injecting-inner-dependencies-when-using-producer-method – paulsm4 Nov 27 '18 at 20:30
  • But I have a producer for authenticatedUser, the problem is when here "return new PacienteResource();" the new object is not created with authenticatedUser, the producer is not producing an authenticatedUser. – Brayme Guaman Nov 27 '18 at 20:43
  • Q: Short of you writing an [MCVE](https://stackoverflow.com/help/mcve) (please do, if you can!), how can we reproduce the problem to help you resolve it? – paulsm4 Nov 27 '18 at 22:38
  • Try to inject [`ResourceContext`](https://docs.oracle.com/javaee/7/api/javax/ws/rs/container/ResourceContext.html) into the parent resource and use `rc.getResource()` to create the resource instead of manually instantiating it. – Paul Samsotha Nov 28 '18 at 03:56
  • 1
    As an aside, your sub resource classes should NOT be annotated with `@Path`. If you have classpath scanning enabled, then it will register the sub resource class as a root resource class (which you do not want). – Paul Samsotha Nov 28 '18 at 03:57
  • @paulsm4 I added more code of my problem! And I deleted Path of my subresource. – Brayme Guaman Nov 28 '18 at 13:54
  • Did you try my suggestion in my previous comment? – Paul Samsotha Nov 29 '18 at 06:09
  • I found the solution, injecting the subresource in the root resource. – Brayme Guaman Nov 29 '18 at 14:46

1 Answers1

0

I found the solution in this way: I inject the object instead of creating a new instance in runtime. I do not understand very well how this work, but this works.

@Path("/medicos")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MedicoResource {

    @Inject//Injection instead of create a new instance.
    PacienteResource pacienteResource;

    @Inject
    @AuthenticatedUser
    Usuario authenticatedUser;

    @GET
    @Path("me")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getMedico() {
        return Response.ok(authenticatedUser.getMedico().getNombre()).build();
    }

    @Path("/me/pacientes")
    public PacienteResource getMedicoPacientes() {
        return PacienteResource;// Problem solved.
    }
}
Brayme Guaman
  • 175
  • 2
  • 12