我有这样的模型课,用于hibernate

@Entity
@Table(name = "user", catalog = "userdb")
@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements java.io.Serializable {

    private Integer userId;
    private String userName;
    private String emailId;
    private String encryptedPwd;
    private String createdBy;
    private String updatedBy;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "UserId", unique = true, nullable = false)
    public Integer getUserId() {
        return this.userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Column(name = "UserName", length = 100)
    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Column(name = "EmailId", nullable = false, length = 45)
    public String getEmailId() {
        return this.emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    @Column(name = "EncryptedPwd", length = 100)
    public String getEncryptedPwd() {
        return this.encryptedPwd;
    }

    public void setEncryptedPwd(String encryptedPwd) {
        this.encryptedPwd = encryptedPwd;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Column(name = "UpdatedBy", length = 100)
    public String getUpdatedBy() {
        return this.updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }
}

在Spring MVC控件中,使用DAO可以获取对象.并作为JSON对象返回.

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUser/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public User getUser(@PathVariable Integer userId) throws Exception {

        User user = userService.get(userId);
        user.setCreatedBy(null);
        user.setUpdatedBy(null);
        return user;
    }
}

View part is done using AngularJS, so it will get JSON like this

{
  "userId" :2,
  "userName" : "john",
  "emailId" : "john@gmail.com",
  "encryptedPwd" : "Co7Fwd1fXYk=",
  "createdBy" : null,
  "updatedBy" : null
}

如果我不想设置加密密码,我会将该字段也设置为空.

But I don't want like this, I dont want to send all fields to client side. If I dont want password, updatedby, createdby fields to send, My result JSON should be like

{
  "userId" :2,
  "userName" : "john",
  "emailId" : "john@gmail.com"
}

我不想从其他数据库表发送到客户端的字段列表.因此,它将根据登录的用户进行更改.我该怎么做?

我希望你明白我的问题.

推荐答案

Add the @JsonIgnoreProperties("fieldname") annotation to your POJO.

或者,在反序列化JSON时,可以在要忽略的字段名称前使用@JsonIgnore.例子:

@JsonIgnore
@JsonProperty(value = "user_password")
public String getUserPassword() {
    return userPassword;
}

GitHub example

Json相关问答推荐

输入请求中不存在null的条件抖动

最新版本的Deneb在数据溢出时不支持滚动

使用单元和非单元版本反序列化Rust中的枚举,而无需编写自定义反序列化程序

如何编写MongoDB查询以返回数组数组

褐煤面积图中的分选问题

如何使用 JOLT 将带有列表的 JSON 项目取消列出为多个项目?

在 NX 工作区中跨多个应用共享 ngx-translate 翻译文件

使用本地 JSON api react TS Axios

Vue 3如何将参数作为json发送到axios get

JOLT 转换 - 删除 JSON 数组中的空 node

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

将 ES6 类对象序列化为 JSON

在 JSON 字符串中react i18n 换行符

.NET CORE 3 升级 CORS 和 Json(cycle) XMLHttpRequest 错误

Django - 异常处理最佳实践和发送自定义错误消息

如何访问 JSON 对象数组的第一个元素?

如何在 Django JSONField 数据上聚合(最小/最大等)?

使用 application/json 优于 text/plain 的优势?

如何从 MySQL 中检索 JSON 数据?

TypeError:使用Python解析JSON时字符串索引必须是整数?