1

Trying to inject a bean inside a webservice I always get NullPointerException.

See:

package br.com.ewatt.webservice;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import br.com.ewatt.bo.MedidorBOImpl;

@Path("")
public class MedidorWs {

    @Inject
    private MedidorBOImpl medidorBO;

    @Path("/receber_corrente/{mac}/{corrente}")
    @GET
    public void receberCorrente(@PathParam("mac") String mac,
            @PathParam("corrente") double corrente) {
        medidorBO.registrarConsumo(mac, corrente);

    }

    public MedidorBOImpl getMedidorBO() {
        return medidorBO;
    }

    public void setMedidorBO(MedidorBOImpl medidorBO) {
        this.medidorBO = medidorBO;
    }

}

I tried use @Named and @ApplicationScoped in MedidorWs class but didn't work, the attribute medidorBO is always null.

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92
  • What container are you using, including version? You have a `beans.xml` in your `WEB-INF` directory? – John Ament Nov 08 '15 at 20:02
  • i'm using tomcat, and yes i have beans.xml in web-inf. Others injections works normally, just in webservice dont work. – Ronaldo Lanhellas Nov 08 '15 at 20:17
  • Please provide more info. CDI implementation? JAX-RS impl? And don't forget versions. – John Ament Nov 08 '15 at 21:52
  • I'm using CDI Weld 2.3.0 Final + Jersey 1.8 – Ronaldo Lanhellas Nov 08 '15 at 22:39
  • My guess is that jersey support for CDI isn't being activated for any number of reasons. If you can update jersey to a reasonably recent version, this post may help you http://stackoverflow.com/questions/17224270/how-to-enable-cdi-inject-in-web-service-jaxrs-jersey-on-java-se-running-grizzl 4 year old versions of libraries are just asking for trouble. – John Ament Nov 08 '15 at 22:46
  • I tried update the jersey to 1.8 but didnt work – Ronaldo Lanhellas Nov 09 '15 at 01:33

0 Answers0