7

对于我的生活,我无法让 Jersey 与 hk2 自动发现 @Service 注释类并注入它们。我已尝试遵循有关堆栈溢出、球衣和 hk2 文档的所有建议,但仍然没有运气。我正在尝试将一个简单的 echo 服务注入到 Jersey 资源中。骨架是从 Jersey 的简单 webapp maven 原型生成的,我尝试对其进行扩展。这是我到目前为止所拥有的:

pom.xml

<build>
  <finalName>sandbox</finalName>
  <plugins>
    <plugin>
      <groupId>org.glassfish.hk2</groupId>
      <artifactId>hk2-inhabitant-generator</artifactId>
      <version>2.3.0</version>
      <executions>
        <execution>
          <configuration>
            <verbose>true</verbose>
          </configuration>
          <goals>
            <goal>generate-inhabitants</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
...
<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>
  <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
  </dependency>
  <dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>hk2</artifactId>
    <version>2.3.0</version>
  </dependency>
</dependencies>

web.xml

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>my.package.jerseytest</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>my.package.jerseytest.application.Application</param-value>
    </init-param>    

    <load-on-startup>1</load-on-startup>
</servlet>

my.package.jerseytest.application.Application

public class Application extends ResourceConfig {
    public Application() {
        ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    }
}

my.package.jerseytest.service.EchoService

@Service
public class EchoService {
    public String generateResponse(String echo) {
        return echo;
    }
}

my.package.jerseytest.resource.MyResource

@Path("myresource")
public class MyResource {

    @Inject
    EchoService echoService;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return echoService.generateResponse("Got it!");
    }
}

我已经检查了抑制剂生成器确实运行并产生了它的输出,但是在运行 Tomcat 服务器 GETting 时,http://localhost:8080/sandbox/webapi/myresource我得到了

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/sandbox] threw exception [A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=EchoService,parent=MyResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,932014249)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of my.package.jerseytest.resource.MyResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on my.package.jerseytest.resource.MyResource
] with root cause
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=EchoService,parent=MyResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,932014249)

有什么我想念的想法吗?我将不胜感激任何帮助 :(

注意!我知道

但他们没有帮助我...

4

5 回答 5

2

我正在结合从这两个问题中获得的见解:

首先,在您的构建链中使用HK2 元数据生成器(或居民生成器)(就像您已经做的那样)。这将扫描您的源并创建META-INF/hk2-locator/default.

其次,创建一个新的ServiceLocator,填充来自元数据的服务:

ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();

现在将其传递Grizzly. 引用@peeskillet

Jersey 有它自己的 ServiceLocator,尝试获取它的引用并不容易。我们可以为 Jersey 提供我们的 ServiceLocator,但 Jersey 最终仍会创建它自己的定位器,并将用我们的定位器填充它。

ResourceConfig config = new MyApplicationConfig();
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
    URI.create(BASE_URI),
    config, 
    serviceLocator
);
于 2015-08-13T11:43:17.747 回答
1

我通过使用扩展 AbstractBinder 的类、实例化它并将其注册到应用程序来解决我的问题。

resourceConfig.register(new DependencyBinder());

还,

/**
 * dependency injection bindings.
 * Jersey requires that service implementations are bound to their contracts this way.
 */
public final class DependencyBinder extends AbstractBinder {

    @Override
    protected final void configure() {
        bind(StatusServiceImpl.class).to(StatusService.class);
    }
}
于 2017-01-04T09:49:44.257 回答
0

使用packages(true, "my.package.jerseytest"); 和使用org.glassfish.jersey.spi.Contractorg.jvnet.hk2.annotations.Contract注释。并使用没有泛型的简单接口。

于 2014-10-07T15:55:46.917 回答
0

尝试在 Application 构造函数中添加需要扫描的包。包上的“true”参数意味着递归地扫描包:

public class Application extends ResourceConfig {
    public Application() {
        packages(true, "my.package.jerseytest");
        ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    }
}
于 2014-10-07T15:50:38.827 回答
0

尝试添加@Stateless

@Path("myresource")
@Stateless
public class MyResource {
    @Inject
    EchoService echoService;
...
}
于 2015-01-14T11:14:21.520 回答