[英]Jersey Guice Integration exception
我正在努力让Guice与我的Jersey / Grizzly班一起工作。 我从控制台Java应用程序开始,添加了Guice,并使注入工作以及域对象正常工作。 然后,我继续通过Jersey / Grizzly添加Web服务。 从您的编码风格可以看出,我来自C#背景。 因此,我敢肯定,我正在努力学习Java的做事方式。
我想要的是我的非webservices类可以注入到webservices处理程序中,以便它们可以使用我构建的功能。
在下面的类中,我有一个数据库实例处理程序想要注入到webservices类中:
@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>
抱歉,我无法用更确切的方式解释此问题,这是错误的地方。 我可能在设计我的应用程序时完全错了,鉴于这只是我的学习,我对此表示满意。 我想避免重写我的域逻辑构造和单元测试。
谢谢您的帮助Matt
java.lang.NoSuchMethodError
绝对是软件包和库版本的错误。 这意味着您已经编译了引用运行时代码中不存在的方法的代码 。
这不是在代码中常见的错误,因为编译器不允许您传递引用不存在的方法的代码。 但是在这种情况下,引用该方法的代码和所引用的代码都是库,因此这意味着具有该方法引用的代码是针对目标类的不同版本编译的。
某种程度上,此错误类似于更常见的ClassNotFoundException
。 但是,不是找不到类,而是要在类内部找不到方法。
现在您知道了问题所在。 恐怕解决起来并不容易。 每年,使用Java进行程序包管理和库解析变得越来越困难。 我看你正在使用泽西库的BOM(物料清单)。 另外,您的pom
文件也不容易。 我建议您仅使用带有Guice和HK2-Guice桥的API(jax-rs)代码的结构来构建测试项目。 也许不是使用BOM表,而是尝试以下最新版本,它们对我有用:
我使用的是servlet容器,但您使用的是独立容器。 没关系,请使用您的,但保留版本号。
另外,请尝试检查最终版本中包含的每个软件包的版本。 您可能会发现此maven命令显示依赖关系树很有用:
https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.