gpt4 book ai didi

java - @JsonIgnore 不起作用

转载 作者:行者123 更新时间:2023-11-30 06:21:43 26 4
gpt4 key购买 nike

由于某种原因,注释@JsonIgnore在我的项目中不起作用。我已经阅读了一些答案,这可能是由于使用不同的不兼容的 Jackson 版本(org.codehaus 与 com.fasterxml)引起的,但我仅通过一个依赖项获取我的 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 库来解决此问题。

关于java - @JsonIgnore 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com