java - Jersey Hk2 注入(inject)@Service注解类

标签 java service jersey inject hk2

对于我来说,我无法让带有 hk2 的 Jersey 自动发现 @Service 注释类并注入(inject)它们。我已经尝试遵循有关堆栈溢出、 Jersey 和 hk2 文档的所有建议,但仍然没有成功。我正在尝试将一个简单的回显服务注入(inject) Jersey 资源。骨架是从 Jersey 的简单 webapp maven 原型(prototype)生成的,我试图扩展它。这是我目前所拥有的:

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>

网络.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>

我的.package.jerseytest.application.Application

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

我的.package.jerseytest.service.EchoService

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

我的.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)

知道我遗漏了什么吗?我将不胜感激 :(

注意!我知道

但他们并没有帮助我...

最佳答案

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

首先,使用 HK2 Metadata Generator (或居民发电机)在你的构建链中(就像你已经做的那样)。这将扫描您的源并创建 META-INF/hk2-locator/default

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

ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();

现在将它传递Grizzly。引用 @peeskillet :

Jersey has it's own ServiceLocator, and it's not easy to try a obtain a reference to it. We could give Jersey our ServiceLocator, but Jersey ultimately still creates it's own locator and will populate it with our locator.

ResourceConfig config = new MyApplicationConfig();
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
    URI.create(BASE_URI),
    config, 
    serviceLocator
);

关于java - Jersey Hk2 注入(inject)@Service注解类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26230642/

相关文章:

java - 从 json post 请求中检索 Java 对象

java - 递归除以 3

服务或 Controller 中的 Spring DTO 验证?

maven - 如何使用 Jersey 轻松运行 Web 应用程序?

service - SOA:跨多个服务联接数据

php - 如何将程序安装为启动时自动启动的服务?

java - JAX-RS 将实体作为 JAXB 对象和字符串获取

java - 如何将油漆(图形g)添加到另一个类中?

java - 将文件上传到 Spring 时出现 net::ERR_CONNECTION_ABORTED 的可能原因是什么

java - 更新 .properties 文件后评论被删除