【问题标题】:JSON with JAXB using Maven使用 Maven 的带有 JAXB 的 JSON
【发布时间】:2012-06-18 15:38:35
【问题描述】:

我正在使用 Jersey(带有 Jetty)和 Maven 来实现 RESTful 服务。我正在尝试使用 JAXB 发送 JSONObject,我遵循了一些教程,例如:http://jersey.java.net/nonav/documentation/1.7/user-guide.html#d4e919http://www.vogella.com/articles/REST/article.html

所以我只有一个简单的 bean,我想将它作为 JSON 对象发送。我已按照所有步骤操作,但无法发送 JSON 对象(尽管我确实正确接收了 XML 格式)。我得到以下异常:com.sun.jersey.api.MessageException:Java 类 de.vogella.jersey.jaxb.model.Todo 和 Java 类型类 de.vogella.jersey.jaxb 的消息正文编写器。 model.Todo,未找到 MIME 媒体类型 application/json

我尝试了各种不同的解决方案,但似乎没有任何效果。 我想知道我是否必须缺少任何依赖项,或者我必须实现一个 ContextResolver(尽管任何教程都将它们用于像这些简单的对象)。

这些是我拥有的依赖项:

    <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- Jersey -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.11</version>
    </dependency>

    <!-- Json -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.11</version>
    </dependency>

    <!-- Jetty -->
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty</artifactId>
        <version>6.1.26</version>
    </dependency>

</dependencies>

非常感谢

【问题讨论】:

    标签: json jaxb jersey jax-rs


    【解决方案1】:

    我找到了缺失的依赖:

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.0.0</version>
            <scope>compile</scope>
        </dependency>
    

    我还必须添加一个存储库以避免丢失原型错误:

    <repositories>
        <repository>
            <id>EclipseLink Repo</id>
            <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>
    

    【讨论】:

    • 是否在非 Oracle JDK 上运行?还没有考虑过。 Oracle JDK 开箱即用地捆绑了 JAXB RI,因此您不需要 eclipse 链接(替代 JAXB impl。)依赖项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2012-11-02
    相关资源
    最近更新 更多