java - Maven + Resteasy JAXB 找不到内容类型 application/xml 的 writer

标签 java xml maven jaxb

我想创建一个将在其他项目中使用的 .jar,其中包括使用 ReastEasy 库(jaxb、jaxrs、jaxrs-clients 等)。

虽然我包含了所有库,并且在基于 Maven 的项目中一切都运行良好,但显然有些库未包含在 .jar 中,并且我在简单的 Java SE 项目中遇到以下异常,其中 .jar 仅包含在lib 文件夹:

RuntimeException: could not find writer for content-type application/xml type

.jar 是使用 Maven 和程序集插件构建的 mvn cleancompile assembly:single

以下是我的pom.xml的相关部分

<dependencies>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.0.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.3.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>2.3.6.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>2.3.6.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.3.6.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>2.3.6.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.6</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.2</version>
        <scope>compile</scope>
    </dependency>

</dependencies>

<build>

    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
        </plugin>

        <plugin>

            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
    </plugins>
</build>

最佳答案

我通过使用 Maven Shade 构建它解决了这个问题。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.google.MainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这里最重要的一行是:

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />

shade插件配置需要the service transformer它合并 the service discovery mechanism 使用的 META-INF/services 文件。

关于java - Maven + Resteasy JAXB 找不到内容类型 application/xml 的 writer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023155/

相关文章:

Java正则表达式换行符与制表符或空格匹配错误

java图像裁剪

python - 基于匹配的 XML 删除元素

c# - 如何在 C#.NET 中替换现有 XML 文件的内容?

java - 如何在maven插件中初始化/运行java项目

java - Maven/ Spring /Java : ClassNotFoundException - but I know class is there

java - 主题订阅者没有收到消息

java - STS工具套件一直挂着

ruby-on-rails - 如何使用在 url 中也有数据的 net/http 发布 xml 数据

java - Maven/导入基础知识 - 找不到类