【问题标题】:Unable to run generated jar from spring-boot jersey无法从 spring-boot jersey 运行生成的 jar
【发布时间】:2017-07-02 07:41:43
【问题描述】:

我无法使用我的 spring-boot with jersey 项目运行生成的 jar 文件。

我遇到的异常是:

Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 1

通过 IDE(运行 Main 类)或使用 spring-boot:run 时,项目可以正常运行

以下是当前设置的详细信息:

包装:

jar

依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jersey</artifactId>
  <version>1.5.1.RELEASE</version>
</dependency>

我的球衣配置(ResourceConfig)设置为扫描包

@Component
public class JerseyConfiguration extends ResourceConfig {

    public JerseyConfiguration() {
        packages(true, "com.my.base.jaxrs.packages");
    }

}

spring-boot-maven-plugin 配置为: org.springframework.boot

<artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

我也没有使用 spring-boot-starter-parent,而是添加了 spring-boot-dependencies,如文档中所示。

【问题讨论】:

  • 您正在执行什么命令来尝试运行生成的 jar ? 请发布所有异常日志
  • java -jar myjarfile.jar 我的用例也与此类似:github.com/spring-projects/spring-boot/issues/3528
  • 你的项目中有MANIFEST.MF 文件吗?
  • 是的。 MANIFEST.MF 包含在 META-INF 中生成的 jar 中
  • 它包含Main-Class 属性?

标签: spring-boot jersey-2.0


【解决方案1】:

我遇到了这个问题,我不想让事情变得太复杂,所以我只是单独注册了我所有的球衣控制器。

@Configuration
public class JerseyConfig extends ResourceConfig {

    JerseyConfig() {

        //  my old version that does not play well with spring boot fat jar        
        /*
            packages(
                    "com.mycompany.api.resources"           
            );
        */

        register(com.mycompany.api.resources.FooController.class);
        register(com.mycompany.api.resources.BarController.class);

}

注意:对于有很多文件的大型项目,我不建议这样做,它很快就会变得冗长、不可读且维护繁琐。 也就是说,这是一个可行的解决方案,您将能够使用通常的 java -jar my-project.jar 命令运行您的 jar。

【讨论】:

    【解决方案2】:

    这更像是一种解决方法,而不是使用的实际有效解决方案 包(真,“我的.package”);

    参考 Anton 的回答,我解决了这个解决方案,但它需要具有类级别 @Path 或 @Provider 注释的资源:

    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
            provider.addIncludeFilter(new AnnotationTypeFilter(Path.class));
            provider.addIncludeFilter(new AnnotationTypeFilter(Provider.class));
            provider.findCandidateComponents("my.package.here").forEach(beanDefinition -> {
                try {
                    LOGGER.info("registering {} to jersey config", beanDefinition.getBeanClassName());
                    register(Class.forName(beanDefinition.getBeanClassName()));
                } catch (ClassNotFoundException e) {
                    LOGGER.warn("Failed to register: {}", beanDefinition.getBeanClassName());
                }
            });
    

    【讨论】:

    • 我找到了相同的解决方案(以前在这里回答:stackoverflow.com/a/42281515/1732450)。类级别的@Path@Provider 注释并不是真正的限制。请参阅org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener 的来源。它是库存解决方案,它还扫描与解决方法相同的类。
    【解决方案3】:

    你也可以这样做,

    @Configuration
    public class JerseyConfig extends ResourceConfig {
    
        JerseyConfig() {
            BeanConfig beanConfig = new BeanConfig();
            beanConfig.setResourcePackage("com.mycompany.api.resources");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2019-08-23
      • 1970-01-01
      相关资源
      最近更新 更多