将json对象消费到具有json数组作为哈希集的类中

时间:2018-08-01 08:31:57

标签: java json hashset

问题是,如何读取json对象中的数组以自动设置权限?

我正在尝试将REST主体中收到的json对象转换为USER类的对象:

public class User {
    public String userid;
    public String password;
    private String fullname;
    private String email;
    private String websettings;
    private String productionlines;
    private String language;
    private String statustracking;
    private String createdate;
    public Set<Authority> authorities;
}

Authority.java:

public final class Authority implements Serializable {


    private String authority;
    private String userid;

    public Authority(String authority, String userid) {
        this.authority = authority;
        this.userid = userid;
    }}

这是我得到的json对象:

{
   "userid":"xx.xx",
   "password":"",
   "fullname":"asdfasdf",
   "email":"ali@sdf.com",
   "websettings":"this is setting",
   "productionlines":"production lines.",
   "createdate":"1234345",
   "statustracking":"status tracking...",
   "authorities" : [
    "user", "admin"
    ]
}

这就是我的消费方式:

   public User createUser(@Valid @RequestBody User user) {
        return userService.createUser(user.getUserid(), user.password, user.getFullname(), user.getEmail(), null, getUserAuthorities(user));
    }

1 个答案:

答案 0 :(得分:2)

可以请您尝试使用以下JSON

{
        "userid" : "xx.xx",
        "password" : "",
        "fullname" : "asdfasdf",
        "email" : "ali@sdf.com",
        "websettings" : "this is setting",
        "productionlines" : "production lines.",
        "createdate" : "1234345",
        "statustracking" : "status tracking...",
        "authorities" : [{
                "authority" : "user",
                "userid" : "userID"
            }
        ]
    }

并提供必要的构造函数,getter / setter并覆盖必要的方法(哈希码/等于)