简体   繁体   English

泽西岛配置无法识别服务和dao类

[英]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 这里的employeeService autowiring给出了一个空指针异常

@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. 我已经尝试了所有操作在我的employeeServiceImpl我有@service注释仍然无法正常工作。

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 . 要使用内置的DI框架(HK2)配置依赖项注入,您应该使用AbstractBinder ,如在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. 其次,您不使用@Autowired批注。 This annotation is specifically for Spring. 该批注专门用于Spring。 For standard injection with Jersey, just use the @Inject annotation. 对于使用Jersey进行标准注入,只需使用@Inject批注。 Also remove the @Component annotation, as this is also for Spring. 还要删除@Component注释,因为这也适用于Spring。

As an aside, if you do want to integrate Spring with Jersey, you should read Why and How to Use Spring With Jersey . 顺便说一句,如果您确实想将Spring与Jersey集成在一起,则应该阅读为什么和如何将Spring与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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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