代码之家  ›  专栏  ›  技术社区  ›  Marci-man

将json对象作为hashset使用到json数组的类中

  •  1
  • Marci-man  · 技术社区  · 6 年前

    问题是,如何将json对象中的数组自动读取到权限集?

    我试图将rest body中接收到的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 回复  |  直到 6 年前
        1
  •  2
  •   mcacorner    6 年前

    你能试试下面的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和重写必要的方法(hashcode/equals)