如何在 Jersey Rest 中忽略和编辑 JSON 对象的属性 [英] How to ignore as well as property editing for JSON objects in Jersey Rest

查看:70
本文介绍了如何在 Jersey Rest 中忽略和编辑 JSON 对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我要求的一部分,我公开了一个 Web 服务,该服务将 Employee 类作为 JSON 格式的输入.Employee类如下.如果您看到类中有 3 个属性,例如状态、密码、创建时间.现在我试图阻止用户提供诸如状态和创建时间之类的属性.我的意思是说我不想让用户输入 JSON 为:-

As part of my requirement I am exposing a web service which takes a Employee class as input in JSON format. Employee class as follows. If you see there are 3 properties inside the Class like status, password, creationTime. Now I am trying to stop user from giving properties such as status and creationTime. I mean to say I dont want to allow user to input the JSON as:-

{
  "emp_id": "xyz@gmail.com",
  "credentials" : {"password": "xxxxx"},
  "status": "ACTIVE",
  "creationTime": "<UTC time>"
}

当输入 status 和 creationTime 时,它​​应该会导致 400 错误消息.同样,当我将结果显示回用户时,例如 return Response.status(Status.ACCEPTED).entity(employee).build(); 它不应该显示 creationTime 或凭据.它应该看起来像:-

When status and creationTime are entered it should result in 400 error message. Similarly when I display the result back to user something like return Response.status(Status.ACCEPTED).entity(employee).build(); it should not display creationTime or credentials. it should look like :-

 {
      "emp_id": "xyz@gmail.com",
      "status": "ACTIVE",
    }

我可以看到有一个 @JsonIgnore 属性在我的状态下不起作用.我试过杰克逊.

I could see that there is a @JsonIgnore property which is not working in my case for status. I tried jackson.

我的Employee类如下:

My Employee class is as follows:

import java.util.Date;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

@XmlRootElement
public class Employee {
    @XmlElement(name = "emp_id", required = true)
    @JsonProperty("emp_id")
    private String empId;
    private Credentials credentials;
    private String status;
    private Date creationTime;

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public Credentials getCredentials() {
        return credentials;
    }

    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }

    @JsonIgnore
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

}

推荐答案

Jersey 默认 JSON provider

来自泽西岛文档:

通过 MOXy 支持 JSON 绑定是自 Jersey 2.0 以来在您的 Jersey 应用程序中支持 JSON 绑定的默认和首选方式.当 JSON MOXy 模块位于类路径上时,Jersey 将自动发现该模块并通过您的应用程序中的 MOXy 无缝启用 JSON 绑定支持.

JSON binding support via MOXy is a default and preferred way of supporting JSON binding in your Jersey applications since Jersey 2.0. When JSON MOXy module is on the classpath, Jersey will automatically discover the module and seamlessly enable JSON binding support via MOXy in your applications.

由于 MOXy 支持 JAXB注解,尝试使用@XmlTransient.它应该可以解决问题.

Since MOXy supports JAXB annotations, try using @XmlTransient. It should do the trick.

要将 Jackson 2.x 用作您的 JSON 提供程序,您需要添加 jersey-media-json-jackson 模块到您的 pom.xml 文件:

To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson module to your pom.xml file:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.22.1</version>
</dependency>

要使用 Jackson 1.x,您需要 jersey-media-json-jackson1 模块:

To use Jackson 1.x you'll need the jersey-media-json-jackson1 module:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson1</artifactId>
    <version>2.22.1</version>
</dependency>

有关依赖项的更多信息,请查看 Jersey 文档.

For more information about the dependencies, have a look at Jersey documentation.

如果可以,请选择 Jackson 2.x 而不是 Jackson 1.x.

If you can, choose Jackson 2.x over Jackson 1.x.

为了使用 Jackson 作为您的 JSON 提供程序,您需要注册 JacksonFeature 用于 Jackson 2.x(或 Jackson1Feature for Jackson 1.x) 在您的 ResourceConfig 类(泽西岛自己的 Application 类):

In order to use Jackson as your JSON provider you need to register JacksonFeature for Jackson 2.x (or Jackson1Feature for Jackson 1.x) in your ResourceConfig class (Jersey's own implementation of Application class):

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        register(JacksonFeature.class);
    }
}

有关更多详细信息,请查看文档.

For more details, have a look at the documentation.

确保根据 Jackson 版本使用正确的 JsonProperty 注释:

Ensure you are using the correct JsonProperty annotation according to Jackson version:

有关注释的更多详细信息,请查看文档:

For more details about the annotations, have a look at the documentation:

此外,尝试使用正确的 @JsonProperty 注释来注释 status 字段,而不是注释 getStatus() 方法.

Additionally, instead of annotating the getStatus() method, try annotating the status field with the proper @JsonProperty annotation.

这篇关于如何在 Jersey Rest 中忽略和编辑 JSON 对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆