How to use CDI in Webservice?

Asked

Viewed 318 times

-1

I’m trying to inject a bean @Injection within a WebService, but the bean always stays null.

Dependency injection is working in my project. When I use @Injection within a Managebean to a jsp page everything is ok.

@WebService
public class listaUsuariosWS {
    @Inject private LoginService loginService;
    public String getName(){
        List<Usuario> lst = loginService.listarTodos();
        Usuario u = lst.get(1);
        String nome = u.getNomeUsuario();
        return nome;
    }
}

Any idea what it could be?

  • Could you put more information about the problem? The code you are testing, maybe the generated stacktrace

  • @Denisrudneidesouza, the code itself is quite simple:

  • @Denisrudneidesouza, the code itself is quite simple: @WebService&#xA;public class listaUsuariosWS {&#xA; &#xA; @Inject&#xA; private LoginService loginService;&#xA; &#xA; public String getName(){&#xA; &#xA; List<Usuario> lst = loginService.listarTodos();&#xA; Usuario u = lst.get(1);&#xA; String nome = u.getNomeUsuario();&#xA; return nome;&#xA; &#xA; }&#xA;&#xA;} The loginService object always stays null

1 answer

0

The class listUsuariosWS has to be managed by the container for the Bean to be injected. Try to give her a scope using EJB:

@Stateless

or CDI:

@RequestScope

Note: I don’t know if @Requestscope works on @Webservice, I just indicated it to exemplify.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.