gpt4 book ai didi

java - 过滤 JSON 响应 Java Spring - RESTful 服务

转载 作者:行者123 更新时间:2023-12-01 11:22:01 26 4
gpt4 key购买 nike

我试图根据用户是否是内部用户来隐藏 GET 中的某些字段。

这是我的 Controller 的片段:

@RequestMapping(value = "rules", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public StandardJSON list(@ActiveUser ActiveUserId userId)
{
StandardJSON sj = new StandardJSON();
sj.setErrors(new ArrayList<String>());
sj.setData(service.listAllRules());
return sj;
}

上面返回以下 JSON:

{
"customerProfileId": "",
"lastModifiedBy": "",
"location": "",
...
}

我可以使用 getIsInternalUser() 方法确定用户是否是内部用户。

如果用户是外部用户,我将如何过滤 json 项之一?

最佳答案

如果你可以使用Genson - Java and Scala to JSON conversion library那么就可以这样做。它将从 Bar 类中排除“foo”字段。

Genson genson = new Genson.Builder().exclude("foo", Bar.class).create();
genson.serialize(yourObject);

关于java - 过滤 JSON 响应 Java Spring - RESTful 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117436/

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