繁体   English   中英

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

[英]Jersey Configuration Not identifying service and dao classes

这是我的球衣配置类

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

            packages("com.ems");

            register(EmployeeService.class);
        }
    }

这里的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();
    }
}

我已经尝试了所有操作在我的employeeServiceImpl我有@service注释仍然无法正常工作。

要使用内置的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);
            }
        });
    }
}

其次,您不使用@Autowired批注。 该批注专门用于Spring。 对于使用Jersey进行标准注入,只需使用@Inject批注。 还要删除@Component注释,因为这也适用于Spring。

顺便说一句,如果您确实想将Spring与Jersey集成在一起,则应该阅读为什么和如何将Spring与Jersey一起使用 它将分解您需要了解的有关集成两个框架的知识。

您应该注册控制器而不是服务类。 样品

@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