简体   繁体   English

具有来自JSON的选择性JSON属性的REST响应

[英]REST response with selective json properties from json

I have an entity like 我有一个像

public class Pojo{
    int id;
    org.json.JSONObject json;
    /* other properties */
}

Now what I want to do is to send this Pojo object back as JSON BUT in a selective way so that an admin user has access to all properties and a regular user has selective access. 现在,我要做的是以选择性方式将此Pojo对象作为JSON BUT发送回去,以便admin用户有权访问所有属性,而regular用户则具有选择性访问权限。

I can do that by using Spring JSONView and creating different views for admin and a regular user. 我可以通过使用Spring JSONView并为adminregular用户创建不同的视图来做到这一点。 However, the problem which i am stuck at that how can i add selective properties for admin / regular user from JSONObject ? 但是,我遇到的问题是如何从JSONObjectadmin / regular用户添加选择性属性? The only possible way i could think of is to create a another Pojo to map that object and then use that object in response. 我想到的唯一可能的方法是创建另一个Pojo来映射该对象,然后使用该对象作为响应。 But I want to make sure that there is no other way to approach this. 但我想确保没有其他方法可以解决此问题。

PS: its a spring boot application PS:它是一个春季启动应用程序

I'm not sure if it works with JsonObject but you can give this a try. 我不确定它是否可以与JsonObject一起使用,但是您可以尝试一下。 Would be interesting to know if it works with "dynamic" objects. 知道它是否适用于“动态”对象会很有趣。

https://github.com/Antibrumm/jackson-antpathfilter https://github.com/Antibrumm/jackson-antpathfilter

(I'm the creator of that project) (我是该项目的创建者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM