泽西岛 Guice 集成异常

Jersey Guice Integration exception

我正在努力让 Guice 与我的 Jersey/Grizzly classes 一起工作。我从控制台 Java 应用程序开始,添加了 Guice 并让我的注入工作以及我的域对象。然后我继续通过 Jersey/Grizzly 添加网络服务。正如您从我的编码风格可以看出的那样,我有 C# 背景。所以我确信我的一些努力是学习 Java 做事的方式。

我想要的是我的非网络服务 classes 可以注入到网络服务处理程序中,以便它们可以使用我构建的功能。

在下面的 class 中,我有一个数据库实例处理程序,我想将其注入 Web 服务 classes:

@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 class 扩展中。但无论我使用什么版本,当我尝试初始化 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>

抱歉,我无法更明确地解释这个问题,这就是问题所在。我可能完全错误地构建了我的应用程序,鉴于这只是我的学习,我对此没有意见。我想避免重写我的域逻辑构造和单元测试。

感谢您的帮助 马特

错误解释

A java.lang.NoSuchMethodError 绝对是包和库版本的错误。这意味着 您编译的代码引用了运行时代码中不存在的方法

这不是代码中常见的错误,因为编译器不会让您传递引用不存在方法的代码。但是在这种情况下,引用该方法的代码和被引用的代码都是库,因此这意味着带有方法引用的代码是针对不同版本的目标编译的 class.

这个错误有点类似于更常见的 ClassNotFoundException。但是不是找不到 class 你没有在 class.

中找到方法

找到问题的根源

现在你知道问题出在哪里了。恐怕要解决它并不是那么容易。 Java 的包管理和库解析每年都变得越来越难。我看到您正在使用 Jersey 库的 bom(物料清单)。此外,您的 pom 文件并不容易。我建议您使用 Guice 和 HK2-Guice 桥仅使用 API (jax-rs) 代码的结构构建一个测试项目。也许不使用 BOM,试试这个最新版本,它们对我有用:

  • com.google.inject:guice:4.1.0
  • org.glassfish.jersey.containers:jersey-container-servlet:2.25
  • org.glassfish.hk2:guice-bridge:2.5.+

我正在使用 servlet 容器,但您使用的是独立容器。没关系,用你的,但保留版本号。

Maven解析

同时尝试检查您在最终构建中包含的每个包的版本。您可能会发现这个 maven 命令对显示依赖关系树很有用:

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