简体   繁体   English

@Inject CDI无法与JAX-RS一起使用

[英]@Inject CDI not working with JAX-RS

I'm trying to create a simple REST service with JAX-RS (Jersey), without using Spring. 我正在尝试使用JAX-RS(Jersey)创建一个简单的REST服务,而不使用Spring。 I want to have the typical structure of: Resource, that use a Service (typical interface with method findById , findAll...), and that Service injected in the Resource. 我想具有以下典型结构:资源,使用服务(具有方法findById典型接口,findAll ...),以及将服务注入资源中。

It seems that CDI automatically scans beans and injects them (having a empty beans.xml in the project) but... it doesn't work for me. 似乎CDI会自动扫描bean并将其注入(在项目中有一个空的beans.xml ),但是...对我来说不起作用。

This is my Resource class: 这是我的Resource类:

@Path("users")
@ManagedBean
public class UserController {

    @Inject
    private UserService userService;

    @GET()
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public User getUserById(@PathParam("id") Long id) {

        return userService.findById(id);
    }
}

And this is my Service and its impl class (it´sa mock...): 这是我的Service及其impl类(它是一个模拟...):

public interface UserService {

    User findById(Long id);

    List<User> findAll();

    User save(User user);

    User update(User user);

    void delete(Long id);
}

public class UserServiceMock implements UserService {

    // omitted constants

    @Override
    public User findById(Long id) {
        return new User()
                .setId(id)
                .setName(NAME_GANDALF)
                .setPhone(PHONE_666554433)
                .setType(TYPE_1)
                .setBirthDate(LocalDate.parse(STRING_DATE_19110102));
    }

    @Override
    public List<User> findAll() {
        return Arrays.asList(
                new User()
                        .setId(USER_ID_12)
                        .setName(NAME_GANDALF)
                        .setPhone(PHONE_666554433)
                        .setType(TYPE_1)
                        .setBirthDate(LocalDate.parse(STRING_DATE_19110102)),
                new User()
                        .setId(USER_ID_140)
                        .setName(NAME_ARAGORN)
                        .setPhone(PHONE_661534411)
                        .setType(TYPE_1)
                        .setBirthDate(LocalDate.parse(STRING_DATE_19230716)),
                new User()
                        .setId(USER_ID_453321)
                        .setName(NAME_FRODO)
                        .setPhone(PHONE_666222211)
                        .setType(TYPE_2)
                        .setBirthDate(LocalDate.parse(STRING_DATE_19511124))
        );
    }

    @Override
    public User save(User user) {
        return user.setId(USER_ID_453321);
    }

    @Override
    public User update(User user) {
        return user;
    }

    @Override
    public void delete(Long id) {
        // delete user by id
    }
}

And I'm using a "no web.xml" configuration, with this class: 我在此类中使用“ no web.xml”配置:

@ApplicationPath("api")
public class RestApplication extends ResourceConfig {

}


The only workaround I found is to "register" the service in the RestApplication class: 我发现的唯一解决方法是在RestApplication类中“注册”服务:

@ApplicationPath("api")
public class RestApplication extends ResourceConfig {

    public RestApplication() {
        register(UserController.class);

        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(new UserServiceMock()).to(UserService.class);
            }
        });
    }
}


Is there another solution to this problem? 这个问题还有其他解决方案吗? I'd rather not to register all my services and other stuff in this class manually... I tried with annotations like @Default , @Qualifier and more (in the service), and no one works... 我宁愿不手动注册我的所有服务和其他的东西在这个类......我试图像注释@Default@Qualifier多(在服务),并且没有一个作品...

Tomcat is a servlet container only. Tomcat只是一个servlet容器。 It's not a full Java EE enterprise stack, nor does it contain a CDI container. 它不是完整的Java EE企业堆栈,也不包含CDI容器。 Apache TomEE , on the other hand, is Tomcat with EE capabilities. 另一方面, Apache TomEE是具有EE功能的Tomcat。

Bauke Scholtz, the infamous BalusC, wrote an excellent blog on installing CDI on Tomcat . 臭名昭著的BalusC的Bauke Scholtz撰写了一篇关于在Tomcat安装CDI的优秀博客。 This might help you. 这可能对您有帮助。 Otherwise, an alternative approach will be to install Apache TomEE and run your application from there. 否则,另一种方法是安装Apache TomEE并从那里运行您的应用程序。 After all, it's still Tomcat anyway. 毕竟,它仍然是Tomcat。

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

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