简体   繁体   中英

Jersey Configuration Not identifying service and dao classes

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.

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.

You should register Controller not Service class. Sample

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

            packages("com.ems");

            register(EmployeeRestController.class);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM