CDI注入在WAS Liberty的REST资源中不起作用,并且以Jersey作为JAX-RS实现

问题描述 投票:0回答:1

我使用的是Websphere自由19.0.0.8,我想使用Jersey而不是默认的CXF来实现jax-rs。我从服务器xml中删除了jaxrs-2.1功能,并在我的webapp .war中打包了球衣实现jar。

<featureManager>
    <feature>servlet-4.0</feature>
    <feature>jndi-1.0</feature>
    <feature>requestTiming-1.0</feature>
    <feature>monitor-1.0</feature>
    <feature>localConnector-1.0</feature>
    <feature>restConnector-2.0</feature>

<!-- Do not add enabled webProfile-8.0 because we want to disable default 
    REST implementation (Apache-CXF) provided by Liberty. We want to use Jersey 
    as our REST implementation because it better support multi-part streaming, -->
    <!-- <feature>webProfile-8.0</feature> -->
    <feature>jsp-2.3</feature>
    <feature>cdi-2.0</feature>
    <feature>managedBeans-1.0</feature>
    <feature>jdbc-4.2</feature>
    <!-- <feature>jaxrs-2.1</feature> -->
</featureManager>

包括球衣实施的版本构建

//JxRS Jersey implementation    
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.25.1'       
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.25.1'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-multipart', version: '2.25.1'
compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: '2.9.0'

扩展球衣的ResourceConfig来配置我的RestApplication

@ApplicationPath("/")
public class RestApplicationConfig extends ResourceConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestApplicationConfig.class);

    public RestApplicationConfig() {
        super();
        configureResourcesAndFeatures();
    }

    private void configureResourcesAndFeatures() {
        packages(RestApplicationConfig.class.getPackage().getName());
        register(MultiPartFeature.class);
    }
}

通过所有这些设置,我的其余api可以正常工作,并且我可以在代码中利用Jersey的多个相关类。

现在问题出在CDI。在我的资源类中,我可以注入CDI管理的资源/类,例如

@ApplicationScoped
@Path("/ping")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PingResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(PingResource.class);

    @Resource(lookup = "jndi_dpa_iss_rest_url")
    private String issRestBaseUrlInResource;

    @Inject
    private DocumentService documentService;
}

在上面的类中,@ Resource和@Inject无法解析JNDI资源和托管bean。一旦我在server.xml中启用jaxrs-2.1功能,CDI注入就会起作用,但是随后我松了球衣,它使用了CXF。

DocumentService及其实现类如下定义。一切都与RestApplicationConfig类位于同一包中,或者位于其子包中。

@ApplicationScoped
@Transactional(value = Transactional.TxType.NOT_SUPPORTED)
public class DocumentServiceImpl implements DocumentService {
    // some code here
}

我需要在其余资源类中使用CDI吗?

jax-rs jersey-2.0 websphere-liberty open-liberty java-ee-8
1个回答
0
投票

由于目前没有CDI 2.0的运动衫扩展名,我不得不找到解决方法。解决方法是手动查询CDI容器以查找我们感兴趣的bean的类型。通过这种方式,我们可以在资源类中手动注入CDI bean,但是注入的bean是托管bean实例,因此CDI会尽力满足其所有依赖关系。 >

这是我们仅在资源层中进行手动注入,但是CDI对于层向下应该可以正常工作。

工作代码。

@ApplicationScoped
@Path("/ping")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PingResource {

    private DocumentService documentService = CDI.current().select(DocumentService.class).get();

}

基本上代替@Inject手动查询CDI容器。

© www.soinside.com 2019 - 2024. All rights reserved.