1

My @Inject not work with my web service (@Path), but work with @WebServlet, why?

my web service

@Path("/autenticacao")
public class UsuarioService {

    @Inject
    private UsuarioRepository usuarioRepository;

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response autentica(@FormParam(value = "cpf") String cpf, @FormParam(value = "senha") String senha){
        Usuario usuario = usuarioRepository.getUsuarioByCPFSenha(cpf, senha);
        return Response.status(200).entity(usuario).build();
    }

}

Definition of the UsuarioRepository interface

public interface UsuarioRepository {

    public Usuario getUsuarioByCPFSenha(String cpf, String senha);

}

my implementation of UsuarioRepository interface

public class UsuarioRepositoryImp implements UsuarioRepository {

    @Inject
    private Connection connection;

    @Override
    public Usuario getUsuarioByCPFSenha(String cpf, String senha) {
        String SQL = "SELECT se01_cpf, se01_senha FROM se01_usuario WHERE replace(replace(se01_cpf,'.',''),'-','') = ? AND se01_senha = ?";

        Usuario usuario = null;

        try {
            PreparedStatement stmt = connection.prepareStatement(SQL);
            stmt.setString(1, cpf);
            stmt.setString(2, senha);

            ResultSet set = stmt.executeQuery();

            while(set.next()){
                String c = set.getString("se01_cpf");
                String s = set.getString("se01_senha");

                usuario = new Usuario(c, s);
            }

            set.close();
            stmt.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return usuario;
    }

}

my field usuarioRepository is null after my requests, any idea?

Yasel
  • 2,920
  • 4
  • 40
  • 48
user155542
  • 471
  • 3
  • 5
  • 19

1 Answers1

1

Jersey dependency injection is based on HK2 and not CDI. As a consequence you need to have a bridge between the two. This is what the jersey-gf-cdi is for:

<dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi</artifactId>
</dependency>

You only need to have that JAR in the classpath. You can see here a configuration for Jetty here: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/pom.xml

And hereafter an example of CDI bean injection into JAX-RS resource: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/src/main/java/io/astefanutti/cdeye/web/BeansResource.java

Antonin Stefanutti
  • 1,061
  • 6
  • 6
  • im using Tomcat 7, now i have other error with jersey-gf-cdi in classpath.. java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder; javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119) com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) – user155542 Mar 18 '15 at 17:40
  • Make sure you're using only one version of Jersey, see: http://stackoverflow.com/questions/23277429/exception-in-rest-jersey – Antonin Stefanutti Mar 19 '15 at 09:33