5

我想我遇到了以下问题Jersey 并不总是适用于 Spring Boot fat jars。解决方法应该是在 to 中设置Jersey依赖POMrequiresUnpack

我的POM样子是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<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>net.hagstrom</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>org.glassfish.jersey.containers</groupId>
                            <artifactId>jersey-container-servlet</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>org.glassfish.jersey.core</groupId>
                            <artifactId>jersey-client</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
                <version>1.4.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

但是当我尝试运行我构建的 JAR 时仍然出现以下错误mvn package

2017-01-13 10:44:28.229 ERROR 9289 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter    : 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; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jerseyConfig' defined in URL [jar:file:/home/mikael/Dev/Java/Java%20Programs/springBootDemo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/net/hagstrom/JerseyConfig.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.hagstrom.JerseyConfig]: Constructor threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mikael/Dev/Java/Java Programs/springBootDemo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)

运行我在 IDE 中使用 Artifacts 构建的 JAR 效果很好。

POM或我用 Maven 构建 JAR 的方式有什么问题吗?

4

3 回答 3

2

这可能已经修复了。我已经发布了一些与使用创建 API 相关的博客文章Spring BootJersey 2Docker使用以下网址记录它们:http Swagger: //tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger- and-Docker.htmlhttp://tech.asimio.net/2016/05/07/Documenting-multiple-REST-API-versions-using-Spring-Boot-Jersey-and-Swagger.html,均附有源代码代码,我不需要unpackrepackage Jersey 2依赖项。

另一方面,当我为另一个关于使用 Spring Cloud、Eureka、Ribbon 和 Feign 的服务注册和发现的博客编写随附的源代码时,我正在集成Spring Boot并且Jersey 1Jersey 1没有Spring Boot我记得要解决的启动器解压缩为该特定 API 服务Jersey 1创建多模块项目所需的依赖项。Maven

于 2017-01-13T15:50:36.857 回答
1

我发现了这个问题。

Spring Boot 1.4 更改了内部 Jar 结构以促进 Spring Boot 引导过程。

https://github.com/spring-projects/spring-boot/issues/1468#issuecomment-267357809

您可以离开此版本 1.4.3:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

但是 spring-boot-jersey fat-jar 要在没有错误的情况下可执行,它需要像这样将插件版本降级到 1.3.8:

     <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.8.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
于 2017-01-18T11:34:27.680 回答
1

问题是 Jersey 无法扫描新的“fat boot jar”中的类。当您尝试使用类的packages("some.package.to.scan")方法时会发生这种情况ResourceConfig

但是,您可以使用 Spring 类路径扫描工具获得相同的效果。这样你就可以像 config.packages() 一样扫描一个包:

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Provider.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(Path.class));
config.registerClasses(scanner.findCandidateComponents("your.package.to.scan").stream()
            .map(beanDefinition -> ClassUtils.resolveClassName(beanDefinition.getBeanClassName(), config.getClassLoader()))
            .collect(Collectors.toSet()));

注:请看源码org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener。这是常用的解决方案,您可以看到它的作用相同:它扫描用@Pathor注释的类@Provider(但由于扫描机制损坏,无法找到任何东西)。

(使用旧版本的引导插件也对我有用,但我尽量避免使用它。)

于 2017-02-16T18:13:38.993 回答