You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala Maven项目构建的Fat Jar运行时抛出RESTEASY异常

Scala + Maven Fat Jar运行时Keycloak REST API序列化异常问题

我用Maven构建Scala项目,目标是生成可命令行运行的Fat Jar。程序通过Keycloak库的REST API创建用户,在IntelliJ中直接运行正常,但编译运行Jar时抛出以下异常,推测是Maven配置错误或缺失依赖:

Exception in thread "main" javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/x-www-form-urlencoded type: javax.ws.rs.core.Form$1
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:328)
[...]

当前pom.xml配置:

<?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>org.generator</groupId>
  <artifactId>data-generator</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <exec.mainClass>com.generator.GeneratorApp</exec.mainClass>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.13.8</scala.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.keycloak</groupId>
      <artifactId>keycloak-admin-client</artifactId>
      <version>18.0.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.4.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.3.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.21.0</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.generator.GeneratorApp</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

编译运行命令:

mvn clean compile assembly:single     

java -cp ./target/data-generator-1.0-SNAPSHOT-jar-with-dependencies.jar com.generator.GeneratorApp

我之前用SBT做Scala开发,Maven经验有限,搜过资料没解决问题。


解决方法

这个异常的核心原因是Maven Assembly插件打包Fat Jar时,会覆盖META-INF/services目录下的SPI配置文件,而Resteasy需要通过这些文件加载处理application/x-www-form-urlencoded类型的MessageBodyWriter。换成Maven Shade插件即可解决,它能合并这些关键配置文件。

步骤1:替换打包插件

将pom.xml中的maven-assembly-plugin替换为maven-shade-plugin,并添加SPI文件合并的转换器:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.4.1</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <!-- 合并META-INF/services下的SPI配置文件 -->
          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
          <!-- 指定主类 -->
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.generator.GeneratorApp</mainClass>
          </transformer>
        </transformers>
        <!-- 可选:排除重复签名文件,避免打包警告 -->
        <filters>
          <filter>
            <artifact>*:*</artifact>
            <excludes>
              <exclude>META-INF/*.SF</exclude>
              <exclude>META-INF/*.DSA</exclude>
              <exclude>META-INF/*.RSA</exclude>
            </excludes>
          </filter>
        </filters>
      </configuration>
    </execution>
  </executions>
</plugin>

步骤2:重新编译打包

执行以下命令打包:

mvn clean package

步骤3:运行Fat Jar

生成的Jar文件位于target/data-generator-1.0-SNAPSHOT.jar,直接运行:

java -jar ./target/data-generator-1.0-SNAPSHOT.jar

补充说明

如果替换插件后仍有问题,可尝试显式添加Resteasy的表单处理依赖(版本需与Keycloak 18.0.2依赖的Resteasy版本匹配,可通过mvn dependency:tree查看):

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-form</artifactId>
  <version>6.2.1.Final</version>
  <scope>compile</scope>
</dependency>

内容的提问来源于stack exchange,提问作者Pan Wolodyjowsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 08:09:22