英文:
How do I know what implementation of jaxrs I am using?
问题
我有一个使用Java 1.7的应用程序。它具有一些RESTful API。
我想要向一个字段添加@JsonIgnore
注解,以便在API中不返回它。
例如:
Member.java
import com.fasterxml.jackson.annotation.JsonIgnore;
private java.lang.String username;
@JsonIgnore
private java.lang.String password;
但是这并没有忽略密码。
"member": {
"password": "**************",
"username": "richard"
}
我认为@JsonIgnore
不起作用的原因是因为我使用了com.fasterxml.jackson.annotation.JsonIgnore
。我应该使用来自不同库的不同注解吗?即,我的JAX-RS实现可能不是com.fasterxml.jackson
吗?我该如何判断?
IntelliJ的类路径包括:
(我已尝试使用net.minidev.json.annotate.JsonIgnore
但没有成功)
更多信息:
pom.xml
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>
和
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
这是问题吗?有两个不同版本!我不确定2.0.5版本是从哪里来的,因为它没有在pom中定义。
英文:
I have a Java 1.7 starts application. It does have some RESTful api's.
I would like to add @JsonIgnore
to a filed so that it is not returned in the api.
E.g.
Member.java
import com.fasterxml.jackson.annotation.JsonIgnore;
private java.lang.String username;
@JsonIgnore
private java.lang.String password;
Does not ignore the password.
"member": {
"password": "**************",
"username": "richard"
}
I think the reason why @JsonIgnore
does not work, is because I use com.fasterxml.jackson.annotation.JsonIgnore
. Should I use a different annotation from a different library? i.e. Is my implementation of jaxrs maybe not com.fasterxml.jackson
? How do I tell?
The IntelliJ classpath has:
(I have tried net.minidev.json.annotate.JsonIgnore
with no success)
More info:
pom.xml
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>
and
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
Is this the problem? two versions! I am not sure where the 2.0.5 version comes from, as it is not defined in the pom.
答案1
得分: 1
你在这种情况下不需要使用 @JsonIgnore。你可以简单地省略你不想反序列化的变量(在这种情况下是密码),Jackson 将只返回用户名。
英文:
You don't need a @JsonIgnore in this case. You can simply omit the variable that you don't want deserialized(the password in this case) and jackson will just return you the username.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论