Java GrantedAuthority导致JSON映射异常

Java GrantedAuthority导致JSON映射异常,java,json,spring,Java,Json,Spring,面对这个 无法写入JSON:org.hibernate.collection.internal.PersistentSet无法强制转换为org.springframework.security.core.GrantedAuthority;嵌套异常为com.fasterxml.jackson.databind.JsonMappingException:org.hibernate.collection.internal.PersistentSet不能强制转换为org.springframework.

面对这个

无法写入JSON:org.hibernate.collection.internal.PersistentSet无法强制转换为org.springframework.security.core.GrantedAuthority;嵌套异常为com.fasterxml.jackson.databind.JsonMappingException:org.hibernate.collection.internal.PersistentSet不能强制转换为org.springframework.security.core.GrantedAuthority(通过引用链:java.util.ArrayList[0]->com.example.springboot.model.User[“authorities”])

尝试将用户列表传递到我的html页面时出错

我想关键是角色类的序列化错误,但我不知道如何解决这个问题

两个班级都在下面。 用户和角色类通过多个模型连接

@Entity
@Table(name = "users_for_spring_crud")
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id")
public class User implements UserDetails {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name")
private String name;

@Column(name = "email")
private String email;

@Column(name = "password")
private String password;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
@实体
@表(name=“用户\u用于\u spring\u crud”)
@JsonIdentityInfo(
生成器=ObjectedGenerators.PropertyGenerator.class,
property=“id”)
公共类用户实现UserDetails{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@列(name=“name”)
私有字符串名称;
@列(name=“email”)
私人字符串电子邮件;
@列(name=“password”)
私有字符串密码;
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(name=“user\u role”、joinColumns=@JoinColumn(name=“id”)、inverseJoinColumns=@JoinColumn(name=“role\u id”))
私人设定角色;
@实体
@表(name=“roles”)
@JsonIdentityInfo(
生成器=ObjectedGenerators.PropertyGenerator.class,
property=“id”)
公共类角色实现授权{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“role\u id”)
私人长id;
@列(name=“role”)
私有字符串roleName;
@许多(mappedBy=“角色”)
@杰索尼奥雷
列出用户名单;
以及控制器类中的方法(如果需要)

@GetMapping(value = "/admin", produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public List<User> showUsersTable(@ModelAttribute("user") User user){
List<User> listUsers = userService.listUsers();
return listUsers;
@GetMapping(value=“/admin”,products={MediaType.APPLICATION\u JSON\u value})
@应答器
公共列表showUsersTable(@ModelAttribute(“用户”)用户){
List listUsers=userService.listUsers();
返回列表用户;

考虑过编写自定义序列化程序,但无法确定它应该如何工作。

一如既往,关键在于关注自己的类,特别是重写方法

GetAuthories()方法的正确版本解决了此问题

@Override
public Collection<GrantedAuthority> getAuthorities() {
     List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    for (Role role: roles) {
        grantedAuthorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        role.getPrivileges().stream()
        .map(p -> new SimpleGrantedAuthority(p.getRoleName()))
        .forEach(grantedAuthorities::add);
    }
    return grantedAuthorities;
}
@覆盖
公共收集机构(){
List GrantedAuthories=new ArrayList();
for(角色:角色){
添加(新的SimpleGrantedAuthority(role.getRoleName());
role.getPrivileges().stream()
.map(p->new SimpleGrantedAuthority(p.getRoleName()))
.forEach(授权机构::添加);
}
返回授权机构;
}

感谢您的关注。

一如既往,关键在于关注您自己的类,特别是重写方法

GetAuthories()方法的正确版本解决了此问题

@Override
public Collection<GrantedAuthority> getAuthorities() {
     List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    for (Role role: roles) {
        grantedAuthorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        role.getPrivileges().stream()
        .map(p -> new SimpleGrantedAuthority(p.getRoleName()))
        .forEach(grantedAuthorities::add);
    }
    return grantedAuthorities;
}
@覆盖
公共收集机构(){
List GrantedAuthories=new ArrayList();
for(角色:角色){
添加(新的SimpleGrantedAuthority(role.getRoleName());
role.getPrivileges().stream()
.map(p->new SimpleGrantedAuthority(p.getRoleName()))
.forEach(授权机构::添加);
}
返回授权机构;
}
谢谢你的关注