2017-02-19 73 views
0

我正努力讓Guice與我的澤西/灰熊課程一起工作。我從控制檯Java應用程序開始,添加了Guice,並使我的注入和我的域對象一起工作。然後我開始通過澤西/灰熊添加web服務。正如你可以從我的編碼風格中看出的那樣,我來自C#背景。所以我相信我的一些努力是學習Java的做事方式。Jersey Guice整合異常

我想要的是我的非webservices類可以注入web服務處理程序,以便他們可以使用我構建的功能。

在下面我的課,我有一個數據庫實例句柄我希望注入web服務類:

@Path("/options") 
public class OptionsServices { 

    private IDatabaseService dbService; 

    @Inject 
    public void setService(IDatabaseService svc){ 
     this.dbService = svc; 
    } 


    @GET 
    @Path("{symbol}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Quote getOptionQuote(@PathParam("symbol") String symbol) { 
     // do stuff 
    } 
} 

我嘗試添加在GuiceBridge和有約束力的,在我的擴展ResourceConfig類的。但是,無論我使用什麼版本,當我嘗試初始化Web服務時,都會遇到關於缺少屬性的非常瘋狂的例外情況。只需從我的pom.xml中刪除GuiceBridge即可刪除異常。這似乎是它的版本兼容性問題,但我不知道什麼版本的庫。

Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.hk2.utilities.general.GeneralUtilities.getSystemProperty(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; 
     at org.jvnet.hk2.internal.ServiceLocatorImpl.<clinit>(ServiceLocatorImpl.java:122) 
     at org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl.initialize(ServiceLocatorGeneratorImpl.java:66) 
     at org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl.create(ServiceLocatorGeneratorImpl.java:98) 
     at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:312) 
     at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.create(ServiceLocatorFactoryImpl.java:268) 
     at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:138) 
     at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:123) 
     at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:308) 
     at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:289) 
     at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:334) 
     at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:122) 
     at Application.Server.WebServer.startServer(WebServer.java:40) 
     at Application.Server.WebServer.Start(WebServer.java:45) 
     at Application.Startup.run(Startup.java:68) 
     at Application.Startup.main(Startup.java:87) 

而且我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>tatmancapital</groupId> 
    <artifactId>ServerConsole</artifactId> 
    <version>R1</version> 

    <properties> 
     <jersey.version>2.17</jersey.version> 
    </properties> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.glassfish.jersey</groupId> 
       <artifactId>jersey-bom</artifactId> 
       <version>${jersey.version}</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <dependencies> 
     <!-- https://mvnrepository.com/artifact/com.google.inject/guice --> 
     <dependency> 
      <groupId>com.google.inject</groupId> 
      <artifactId>guice</artifactId> 
      <version>4.1.0</version> 
     </dependency> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.6</version> 
     </dependency> 

     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version> 
      <exclusions> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
      </exclusions>   
     </dependency>  

     <dependency> 
      <groupId>commons-codec</groupId> 
      <artifactId>commons-codec</artifactId> 
      <version>1.3</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-httpclient</groupId> 
      <artifactId>commons-httpclient</artifactId> 
      <version>3.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.thoughtworks.xstream</groupId> 
      <artifactId>xstream</artifactId> 
      <version>1.4.9</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-lang</groupId> 
      <artifactId>commons-lang</artifactId> 
      <version>2.4</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-logging</groupId> 
      <artifactId>commons-logging</artifactId> 
      <version>1.0.4</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-grizzly2-http</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-moxy</artifactId> 
     </dependency> 


     <dependency> 
      <groupId>org.glassfish.hk2</groupId> 
      <artifactId>guice-bridge</artifactId> 
      <version>2.5.0-b32</version> 
     </dependency>  

     <dependency> 
      <groupId>commons-httpclient-ssl-contrib</groupId> 
      <artifactId>commons-httpclient-ssl-contrib</artifactId> 
      <version>3.1</version> 
      <scope>system</scope> 
      <systemPath>${project.basedir}/libs/commons-httpclient-contrib-ssl-3.1.jar</systemPath> 
     </dependency> 

     <dependency> 
      <groupId>etrade</groupId> 
      <artifactId>com.etrade.accounts</artifactId> 
      <version>1.0</version> 
      <scope>system</scope> 
      <systemPath>${project.basedir}/libs/etws-accounts-sdk-1.0.jar</systemPath> 
     </dependency> 

     <dependency> 
      <groupId>etrade</groupId> 
      <artifactId>com.etrade.order</artifactId> 
      <version>1.0</version> 
      <scope>system</scope> 
      <systemPath>${project.basedir}/libs/etws-order-sdk-1.0.jar</systemPath> 
     </dependency> 

     <dependency> 
      <groupId>etrade</groupId> 
      <artifactId>com.etrade.oauth</artifactId> 
      <version>1.0</version> 
      <scope>system</scope> 
      <systemPath>${project.basedir}/libs/etws-oauth-sdk-1.0.jar</systemPath> 
     </dependency> 

     <dependency> 
      <groupId>etrade</groupId> 
      <artifactId>com.etrade.markets</artifactId> 
      <version>1.0</version> 
      <scope>system</scope> 
      <systemPath>${project.basedir}/libs/etws-market-sdk-1.0.jar</systemPath> 
     </dependency> 

     <dependency> 
      <groupId>etrade</groupId> 
      <artifactId>com.etrade.common</artifactId> 
      <version>1.0</version> 
      <scope>system</scope> 
      <systemPath>${project.basedir}/libs/etws-common-connections-1.0.jar</systemPath> 
     </dependency>    

    </dependencies> 

    <build> 
     <plugins>  
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>3.0.0</version> 
      <configuration> 
       <archive> 
        <manifest>    
        <mainClass>Application.Startup</mainClass>     
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <appendAssemblyId>false</appendAssemblyId> 
       <finalName>ServerConsole-V1</finalName> 
      </configuration> 
      <executions> 
       <execution> 
        <id>make-assembly</id> <!-- this is used for inheritance merges --> 
        <phase>package</phase> <!-- bind to the packaging phase --> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin>  
     </plugins> 
    </build> 
</project> 

我很抱歉,我不能更明確的解釋這個問題,這裏的什麼是錯的。我可能已經將我的應用程序設計爲完全錯誤的,因爲這只是我的學習,我相信這一點。我想避免重寫我的域邏輯構造和單元測試。

謝謝您的幫助 馬特

回答

0

錯誤解釋

一個java.lang.NoSuchMethodError絕對是包和庫版本錯誤。這意味着您編譯的代碼引用了運行時代碼中不存在的方法。

這不是您的代碼中常見的錯誤,因爲編譯器不會讓您傳遞引用不存在的方法的代碼。但在這種情況下,引用方法的代碼和引用的代碼都是庫,所以這意味着具有方法引用的代碼是針對不同版本的目標類進行編譯的。

不知何故,這個錯誤類似於更常見的ClassNotFoundException。但不是找不到課程,而是在課堂內找不到方法。

找到問題的根源

現在您知道問題所在。恐怕解決這個問題並不那麼容易。 Java的包管理和庫解析每年都變得越來越困難。我看到你正在使用澤西圖書館的bom(物料清單)。另外,您的pom文件不是一件容易的事。我建議你只使用Guice和HK2-Guice橋的API(jax-rs)代碼的結構來構建一個測試項目。也許而是採用了BOM試試這個新版本,他們爲我工作:

  • com.google.inject:吉斯:4.1.0
  • org.glassfish.jersey.containers:新澤西州容器的servlet: 2。25
  • org.glassfish.hk2:吉斯橋:2.5 +

我使用的servlet容器但您使用的是獨立的一個。沒關係,使用你的,但保留版本號。

Maven的分辨率

也可嘗試檢查每個要包括在最終版本軟件包的版本。您可能會發現這個有用的Maven命令顯示依賴關係樹:

https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html

+0

這是一個很好的解釋。我很抱歉這是一個新手問題。我如何不使用BOM? – tatmanblue

+0

@tatmanblue簡單。您刪除bom聲明併爲每個依賴項添加一個版本。物料清單只是一個_meta-dependency_,它爲您提供確切的版本。看看''沒有''的聲明。欲瞭解更多信息,請參閱[http://stackoverflow.com/questions/14874966/how-to-use-bom-file-with-maven](This問題)。 – sargue

+0

謝謝。刪除BOM,爲您的依賴項設置特定的版本號,就像您呼出的那樣,解決了我的問題。我現在能夠從瀏覽器訪問我的web服務而不會出錯(我必須爲json序列化添加一個依賴項) – tatmanblue

相關問題