【问题标题】:Unsupported Media Type when using Jersey and Jackson from uberjar从 uberjar 使用 Jersey 和 Jackson 时不受支持的媒体类型
【发布时间】:2014-09-30 17:24:09
【问题描述】:

我目前面临着 Jersey 和 Jackson 的问题,我找不到解决方案:当我尝试从 JSON 序列化 POJO 作为 Jersey-Endpoint 中的 POST 参数时,如果出现以下情况,它会返回错误我从 uberjar 中调用它。如果我在从 eclipse 启动 main-method 后使用相同的 wget-call,一切正常,我得到预期的答案。我搜索了其他人在使用球衣和后参数应用程序类型时遇到问题,例如Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XMLJAX-RS: How to automatically serialize a collection when returning a Response object?,但主要错误是他们没有在调用中指定标题或使用错误的@Consumes-tags ,我没有。根据我的研究,没有人遇到过类似的问题,即从 eclipse 启动服务器时调用有效,但从外部启动服务器时调用无效。

启动服务器的类如下:

public class ServerStarter {
    public static final String BASE_URI = "http://localhost:8282/test/";

    public static void main(String[] args) throws IOException {
        final ResourceConfig rc = new ResourceConfig().packages("starter");
        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
        System.out.println(String.format("Drücke Enter um Server zu beenden.", BASE_URI));
        System.in.read();
        server.stop();
    }
}

Endpoint 看起来像这样:

@Path("testme")
public class Endpoint {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public String testMe(Parameters ep) {
        return ep.toString();
    }
}

而应该是POJO的Parameters-class看起来像这样:

public class Parameters {
    private int x;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }
}

如果我执行 mvn clean package assembly:single,启动服务器然后调用 wget localhost:8282/test/testme --post-data='{"x": 10 }' --header="Content-Type: application/json",它会返回 415 Unsupported Media Type

pom.xml中的依赖如下:

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.13</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>

有谁知道如何解决这个问题?我没有线索,据我所知,eclipse的调用应该类似于uberjar中的调用。

【问题讨论】:

  • 也许自 JAX-RS 1.0 以来语法已经改变,但不应该是 @Consumes 值中的字符串数组,例如@Consumes({MediaType.APPLICATION_JSON}) ?
  • 我遇到了与 Jetty 类似的问题:stackoverflow.com/questions/27971384/…
  • 我遇到了同样的问题,我可以使用stackoverflow.com/questions/27971384/…解决它
  • 感谢您的提示,但不幸的是,这是一个包装问题而不是球衣问题,因此手动激活杰克逊功能不会改变任何事情。

标签: java json eclipse rest maven


【解决方案1】:

这个问题是由 jar 提供的服务 (http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Service_Provider) 引起的。虽然 eclipse 正确组合了它们,但 uberjar-plugins 使它们相互覆盖。一种可能的解决方案是使用 maven-assembly-plugin,使用以下程序集描述符:

<assembly>
    <id>production</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>    
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>classes</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

之后可以使用java -cp lib/:classes/ MainClass启动应用程序。

【讨论】:

  • 仅供参考,您可以使用Maven Shade plugin 而不是maven-assembly-plugin,因为它提供了类重定位功能,以避免类路径中的相同类名冲突。
【解决方案2】:

大卫的分析是正确的。我只想添加一个替代解决方案。您可以使用带有 ServicesResourceTransformer 的 Maven Shade 插件,例如

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
        <transformers>
            <!-- To ensure the original META-INF/services definitions are respected -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
            <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>id.web.michsan.hellotransfer.Application</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>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2016-06-06
    相关资源
    最近更新 更多