@JsonIgnore 不工作

@JsonIgnore is not working

出于某种原因,@JsonIgnore 注释在我的项目中不起作用。 我已经阅读了一些答案,这可能是由于使用不同的不兼容 Jackson 版本(org.codehaus 与 com.fasterxml)引起的,但我只能通过一个依赖项 (afaik) 获取我的 jackson 库,所以应该不是问题。

@Entity
@Table(name = "employee", schema = "klk")
public class Employee implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;
    @Basic
    @Column(name = "firstName", nullable = false)
    private String firstName;
    @Basic
    @Column(name = "lastName", nullable = false)
    private String lastName;
    @Basic
    @Column(name = "password", nullable = false)
    @JsonIgnore
    private String password;
    @Basic
    @Column(name = "token", unique = true)
    @JsonIgnore
    private String token;

Pom.xml:

<dependencies>  
  <!-- https://mvnrepository.com/artifact/com.eclipsesource.jaxrs/jersey-all-->
    <dependency>
        <groupId>com.eclipsesource.jaxrs</groupId>
        <artifactId>jersey-all</artifactId>
        <version>2.22.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.26</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.6.4</version>
    </dependency>

  </dependencies>

输出:

{
    "id":1,
    "firstName": "John",
    "lastName": "Doe",
    "password": "password",
    "token": "token
}

有人知道为什么注释在我的示例中不起作用吗?

虽然,你得到了解决方案,但为了将来参考,我正在写这个答案。

正如@JB Nizet 在他的评论中指出的那样,您使用的不是 Jackson,而是 MOXy。并且,根据 Jersey Documentation

The modules listed below provide support for JSON representations by integrating the individual JSON frameworks into Jersey. At present, Jersey integrates with the following modules to provide JSON support:

  • MOXy

  • Java API for JSON Processing (JSON-P)

  • Jackson

  • Jettison

因此,您需要删除 MOXy 库才能解决此问题。