2017-04-02 59 views
0

我有一個簡單的webapp與澤西2.6,jboss 5.0.0很好。 將所有我的注入類(服務)聲明爲AbstractBinder的方法不是優美的。使用澤西2.x與CDI沒有申報服務進入AbstractBinder

所以我想用CDI注入來管理所有的生命週期bean管理。我已經看到使用@manageBean代替@ScopeResquest是可能的。

> javax.servlet.ServletException: A MultiException has 3 exceptions. 
> They are: 
> 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at 
> Injectee(requiredType=ServiceExemple,parent=RestService,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,2072559159) 
> 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of fr.xx.xx.rest.RestService errors were 
> found 
> 3. java.lang.IllegalStateException: Unable to perform operation: resolve on fr.xx.xx.rest.RestService 
> 
> org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392) 
> org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) 
> org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) 
> org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219) 
> org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 

我的配置POM:

 <dependency> 
      <groupId>org.glassfish.jersey.core</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-servlet</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 

     <dependency> 
     <groupId>org.glassfish.jersey.core</groupId> 
     <artifactId>jersey-client</artifactId> 
     <version>${jersey.version}</version> 
    </dependency> 


     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-jaxrs</artifactId> 
      <version>1.8.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-json-jackson</artifactId> 
      <version>2.6</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-core-asl</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-mapper-asl</artifactId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jaxb-impl</artifactId> 
        <groupId>com.sun.xml.bind</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jettison</artifactId> 
        <groupId>org.codehaus.jettison</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 



     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>3.1.0.GA</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
     <groupId>org.glassfish.jersey.ext</groupId> 
     <artifactId>jersey-bean-validation</artifactId> 
     <version>${jersey.version}</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.hibernate</groupId> 
        <artifactId>hibernate-validator</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>5.0.0.Final</version> 
</dependency> 


      <dependency> 
      <groupId>org.glassfish.jersey.ext.cdi</groupId> 
      <artifactId>jersey-weld2-se</artifactId> 
       <version>2.25</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.ext.cdi</groupId> 
      <artifactId>jersey-cdi1x</artifactId> 
       <version>2.25</version> 
     </dependency> 


    </dependencies> 

    <build> 
     <finalName>RESTfulExample</finalName> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

我的web.xml:

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name>Restful Web Application</display-name> 

    <servlet> 
     <servlet-name>jersey-serlvet</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
<!--  <init-param> --> 
<!--   <param-name>jersey.config.server.provider.packages</param-name> --> 
<!--   <param-value>fr.xx.xx.rest</param-value> --> 
<!--  </init-param> --> 
     <init-param> 
      <param-name>javax.ws.rs.Application</param-name> 
      <param-value>fr.xx.xx.ApplicationRest</param-value> 
     </init-param> 
     <init-param> 
      <param-name>jersey.config.beanValidation.disable.validateOnExecutableCheck.server</param-name> 
      <param-value>true</param-value> 
     </init-param> 

      <init-param> 
      <param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>jersey-serlvet</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

我的類應用程序路徑:我這個錯誤試過

@ApplicationPath("rest") 
public class ApplicationRest extends ResourceConfig { 

    public ApplicationRest() { 
     // Register resources and providers using package-scanning. 
     packages("fr.xx.xx.rest"); 
     packages("org.codehaus.jackson.jaxrs"); 

     register(JacksonFeature.class); 
     register(org.glassfish.jersey.server.validation.ValidationFeature.class); 
     // register(new ApplicationBinder()); => don't want use it 

我簡單的服務等級:

@Default 
public class ServiceExemple { 
... 

,並完成我的資源類:

@Path("/service") 
@ManagedBean 
public class RestService { 
    @Inject private ServiceExemple service; 

... 

我已經在我添加一個空bean.xml WEB-INF

感謝您的幫助

+0

Err JBoss 5.0?你確定? –

+0

是Jboss 5.0.0 GA –

+0

如果您想要自動發現服務,您可能需要將hk2-metadata-generator(https://hk2.java.net/2.5.0-b36/inhabitant-generator.html)與一個Populator(https://hk2.java.net/2.5.0-b36/apidocs/org/glassfish/hk2/api/Populator.html)。您可以通過獲取每個ServiceLocator擁有的DynamicConfigurationService(https://hk2.java.net/2.5.0-b36/apidocs/org/glassfish/hk2/api/DynamicConfigurationService.html)並使用getPopulator方法 – jwells131313

回答

0

首先,注入資源(豆類)和Jersey Endpoint類(注入點)必須是CDI-Aware。它必須由CDI檢測。我們可以使用bean-discovery-mode =「all」 - 然後CDI掃描所有的類或者 bean-discovery-mode =「annotated」並用proper註釋標記我們的類:從這裏:Bean defining annotations。我喜歡@依賴或@RequestScoped

然後,我們必須使用澤西擴展

<dependency> 
    <groupId>org.glassfish.jersey.ext.cdi</groupId> 
    <artifactId>jersey-cdi1x-servlet</artifactId> 
    <version>{version}</version> 
    <scope>runtime</scope> 
</dependency> 

`

到CDI與HK2發現機制連接。 這是Official oracle Guideline