运行 阴影 Jar 时未加载 Jersey 验证

Jersey validation not loaded when running shaded Jar

最近我开始使用 Jersey 3.0.2 独自开发一个全栈项目。 我已将服务配置为使用 HK2 DI 注释和 Maven 将项目隐藏在 Jar 中。

我想添加一些 bean 验证,为此我已将此依赖项包含在 pom.xml

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-bean-validation</artifactId>
        <version>3.0.2</version>
    </dependency>

这是在 Maven 构建中

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.glassfish.hk2</groupId>
                        <artifactId>hk2-metadata-generator</artifactId>
                        <version>3.0.2</version>
                    </annotationProcessorPath>
                    <annotationProcessorPath>
                            <groupId>org.glassfish.jersey.ext</groupId>
                            <artifactId>jersey-bean-validation</artifactId>
                            <version>3.0.2</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>package-name-with-shade</finalName>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>com.package.name.Main</Main-Class>
                                    <Build-Number>1</Build-Number>
                                    <X-Compile-Source-JDK>1.8</X-Compile-Source-JDK>
                                    <X-Compile-Target-JDK>1.8</X-Compile-Target-JDK>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

这就是我的端点之一的样子

@POST
@Operation(summary = "Create a user in the Database",
        responses = {
                @ApiResponse(description = "The created user",
                        content = @Content(mediaType = "application/json",
                                schema = @Schema(implementation = User.class))),
                @ApiResponse(responseCode = "400", description = "User not found")})
public Response postIt(@Valid @NotNull @ValidUser User user) {
    try {
        crud.createUser(user);
        return Response.ok(user).build();
    } catch (Exception e) {
        logger.info(e);
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getCause()).build();
    }
}

如果我尝试通过 IntelliJ 运行 项目,它一切正常,验证也是如此,我可以在日志中看到验证器已正确加载:

INFO  [2021-06-21 11:12:16.645] org.hibernate.validator.internal.util.Version: HV000001: Hibernate Validator 7.0.0.Final

包括几个像这样的调试信息

DEBUG [2021-06-21 11:12:16.735] org.hibernate.validator.internal.engine.ValidatorFactoryConfigurationHelper

但是,当我使用 Maven 和 运行 构建 Fat Jar 时

mvn clean -DskipTests package && java -jar ./target/package-name-with-shade.jar

除了验证之外一切似乎都工作正常:未显示休眠验证程序日志,未抛出任何错误并且验证将无法进行。尽管找到了已编译的 Jar,但似乎代码库中甚至没有验证。

知道我可以尝试什么吗?

谢谢

在您的阴影插件配置中,您缺少 ServicesResourceTransformer

JAR files providing implementations of some interfaces often ship with a META-INF/services/ directory that maps interfaces to their implementation classes for lookup by the service locator. To relocate the class names of these implementation classes, and to merge multiple implementations of the same interface into one service entry, the ServicesResourceTransformer can be used:

for auto-discovery。否则,您将需要手动注册 features/providers,否则将通过自动发现注册。

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
    <!-- other transformers -->
  </transformers>
</configuration>