0

This is my jersey config class

@ApplicationPath("services")
    public class JerseyApplication extends ResourceConfig{
    public JerseyApplication() {

            packages("com.ems");

            register(EmployeeService.class);
        }
    }

Here autowiring of employeeService is giving a null pointer exception

@Path("/ems")
@Component
public class EmployeeRestController {

    @Autowired
    private EmployeeService employeeService;

    @GET
    @Path("/employees")
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public List<Employee> getEmployees() {
        return employeeService.getEmployees();
    }
}

I have tried everything In my employeeServiceImpl I have @service annotation Still, it is not working.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
HMT
  • 2,093
  • 1
  • 19
  • 51

2 Answers2

1

To configure the dependency injection using the built in DI framework (HK2), you should use an AbstractBinder, as mentioned in some answers in Dependency injection with Jersey 2.0.

@ApplicationPath("services")
public class JerseyApplication extends ResourceConfig {

    public JerseyApplication() {

        packages("com.ems");

        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(EmployeeService.class)
                        .to(EmployeeService.class)
                        .in(Singleton.class);
            }
        });
    }
}

Secondly, you do not use the @Autowired annotation. This annotation is specifically for Spring. For standard injection with Jersey, just use the @Inject annotation. Also remove the @Component annotation, as this is also for Spring.

As an aside, if you do want to integrate Spring with Jersey, you should read Why and How to Use Spring With Jersey. It will break down what you need to understand about integrating the two frameworks.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

You should register Controller not Service class. Sample

@ApplicationPath("services")
    public class JerseyApplication extends ResourceConfig{
    public JerseyApplication() {

            packages("com.ems");

            register(EmployeeRestController.class);
        }
    }
olgunyldz
  • 531
  • 3
  • 8
  • Do you know anything about Injection ? beacuse the problem is it is not performing injection of dependencies – HMT Sep 14 '18 at 13:04