【问题标题】:Jersey ignoring Jackson annotations泽西岛忽略杰克逊注释
【发布时间】:2019-01-15 16:02:04
【问题描述】:

我正在开发一个使用 Jersey 和 Jackson 将 Java 对象转换为 JSON 的 Java 项目。几天前,我注意到使用 com.fasterxml.jackson.annotation.JsonProperty 尝试更改 JSON 属性的名称没有效果。这个问题不是很大,所以我忽略了它。今天我创建了与@Entity POJO 之间的 JPA 关系。关系是@OneToMany@ManyToOne。其中一个 POJO 正在转换为 JSON,这会产生无限递归 (StackOverflowError)。我在其中一个字段上附加了一个com.fasterxml.jackson.annotation.JsonIgnore 注释,但就像在此注释被忽略之前一样。我提供了我的pom.xml 的摘录,我认为这些摘录与this answer 相关,与相关问题有关。

<properties>
    <jackson.version>2.8.7</jackson.version>
    <jersey.version>2.25.1</jersey.version>
    <hk2.version>2.4.0</hk2.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.glassfish.hk2</groupId>
            <artifactId>hk2-inhabitant-generator</artifactId>
            <version>${hk2.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate-inhabitants</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<weblogic-application>
    <prefer-application-packages>
        <package-name>com.fasterxml.jackson.*</package-name>
        <package-name>com.sun.jersey.*</package-name>
        <package-name>jersey.repackaged.*</package-name>
        <package-name>org.glassfish.*</package-name>
        <resource-name>META-INF/services/org.glassfish.hk2.*</resource-name>
    </prefer-application-packages>
    <prefer-application-resources>
        <resource-name>META-INF/services/org.glassfish.jersey.*</resource-name>
        <resource-name>org.glassfish.jersey.*</resource-name>
        <resource-name>jersey.repackaged.*</resource-name>
        <resource-name>META-INF/services/org.glassfish.hk2.*</resource-name>
    </prefer-application-resources>
</weblogic-application>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-cdi1x</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-cdi1x-transaction</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-cdi1x-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jdk-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-simple-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers.glassfish</groupId>
            <artifactId>jersey-gf-ejb</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-bean-validation</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-declarative-linking</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-entity-filtering</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-metainf-services</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc-bean-validation</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc-freemarker</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc-jsp</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc-mustache</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-proxy-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-servlet-portability</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-wadl-doclet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-cdi1x-ban-custom-hk2-binding</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-cdi1x-validation</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-weld2-se</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-guava</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-java8</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-jsr166e</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-rxjava</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-jaxb</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>html-json</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jettison</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-processing</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-kryo</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-sse</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth1-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth1-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth1-signature</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth2-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework</groupId>
            <artifactId>jersey-test-framework-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework</groupId>
            <artifactId>jersey-test-framework-util</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-bundle</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-external</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-inmemory</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-jdk-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-jetty</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-simple</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.bundles.repackaged</groupId>
            <artifactId>jersey-guava</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.bundles.repackaged</groupId>
            <artifactId>jersey-jsr166e</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-apache-connector</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-grizzly-connector</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-jetty-connector</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.hk2</groupId>
            <artifactId>hk2-bom</artifactId>
            <version>${hk2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.hk2.external</groupId>
            <artifactId>javax.inject</artifactId>
            <version>${hk2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.hk2</groupId>
            <artifactId>hk2-api</artifactId>
            <version>${hk2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.hk2</groupId>
            <artifactId>hk2-locator</artifactId>
            <version>${hk2.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies></dependencies>

这是有问题的课程:

@Entity
@Table(name = "PAGE")
@XmlRootElement
public class Page implements Serializable {

    //... Id and other fields here

    @JsonIgnore
    @OneToMany(fetch = LAZY, mappedBy = "page")
    private Set<Activity> analyticsActivity;
}

【问题讨论】:

  • 为什么会有这么多不同的 JSON 提供程序依赖项?您应该删除所有这些,除了杰克逊一个。如果您将所有这些都放在类路径中,谁知道哪一个将是默认的。
  • 一般来说,你有很多不必要的依赖。看来您可能是泽西岛或 Maven 的初学者。您需要了解 transitive 依赖项的概念。一些依赖引入很多传递依赖。您列出了很多传递依赖项。它不会影响拥有它们的项目,但它会让没有拥有它们变得更干净。
  • 为什么你们有这么多的测试框架提供者?还有这么多容器?看起来您的项目中几乎有所有可能的 Jersey 依赖项。这是不必要的,令人困惑的,并且很难弄清楚实际用于什么用途。花点时间了解您使用的依赖项实际用于什么。
  • @PaulSamsotha 谢谢保罗。我是 Maven 的初学者,虽然对 Gradle 更有经验,所以其中一些可以翻译。我加入了这个项目,依赖项已经看起来像这样。如果您认为这很糟糕,您应该看到整个 pom.xml,174 个依赖项。不幸的是,在我听取了同事的意见后,我不得不将这个单体的问题提交给 SO;即使我可以自己回答这个问题,它也只对从事类似设计不佳的项目的少数人有用。
  • 我没有注意到所有内容都在&lt;dependencyManagement&gt; 中。我以为他们都在&lt;dependencies&gt;。后者是应用程序实际使用的。话虽如此,仅供参考,您可以使用jersey-bom 将所有这些Jersey 依赖项添加到&lt;dependencyManagement&gt;。这将包括您手动包含的所有内容。见example。对于您的问题,您应该向我们展示的是&lt;dependencies&gt;

标签: java json jackson jersey-2.0


【解决方案1】:

前段时间我有同样的问题注释 JsonIgnoreProperties 用于类范围。在 jackson 上,当您想忽略某些字段时,您需要指定字段名称。

@JsonIgnoreProperties(ignoreUnknown = true, value= {"prev", "next"})
public class Model implements Serializable {
    private Model prev;
    private Model next; ....

Jackson Ignore Annotations example

为了防止“无限递归 (StackOverflowError)”,请确保您没有任何引用,方法调用内部成员,以防止您也可以忽略此字段。 “无限递归(StackOverflowError)”的一个例子

public class Model implements Serializable {
    private Model prev; // reference previous model
    private Model next; // reference next model

这种类型的数据结构会导致无限递归,因为 Jackson 总是获取 prev 或 next 字段并且一次又一次地获取。

【讨论】:

  • 感谢您的回复,但我遇到的问题是 Jackson 注释对生成的 JSON 没有影响。我继续在课堂上测试了@JsonIgnoreProperties,但效果与@JsonIgnore@JsonProperty 类似。
【解决方案2】:

不幸的是,我发现的这个问题的解决方案可能与杰克逊或泽西岛无关。正如有人所说:“这是不必要的,令人困惑的,并且很难弄清楚实际用于什么用途。”在我的问题中,我没有共享整个 174 依赖项 long pom.xml,因此任何人都不可能回答这个问题。我希望有人觉得这很有用。

当我将@XmlTransient 添加到该字段时,它没有产生任何积极的影响。当我将它添加到 getter 和字段时,我能够从生成的 JSON 中隐藏 analyticsActivity。我没有测试将@XmlTransient单独添加到getter是否有效。

import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "PAGE")
@XmlRootElement
public class Page implements Serializable {

    //... Id and other fields here

    @XmlTransient
    @OneToMany(fetch = LAZY, mappedBy = "page")
    private Set<Activity> analyticsActivity;

    @XmlTransient
    public Set<TmptREPDeskAnalytics> getAnalyticsActivity() {
        return analyticsActivity;
    }
}

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2016-08-07
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多