public 属性 在 json 序列化中被 public getter 所掩盖

public property overshadowed by public getter in json serialization

我有这个奇怪的场景 - 我有这个 PlatformUser class 实现了 Principal getName() returns 它的 email。这是授权所必需的。我希望能够基于 public 属性 nameemail 序列化和反序列化 PlatformUser。我应该如何注释我的 class 才能使其工作。作为一种解决方法,我不得不将 属性 name 更改为 fullName 但这违背了我的问题的目的。谢谢!

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.persistence.*;
import javax.security.auth.Subject;  
import java.security.Principal;  
import java.util.*;

@Entity
public class PlatformUser implements Principal {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int id;
    public String name;
    public String email;
    public String role = getUserRole().getRole().toString().toLowerCase();

    @Transient
    private List<GrantedAuthority> authorities;

    public PlatformUser(){}

    public PlatformUser(String email, List<GrantedAuthority> authorities) {
        this.email = email;
        this.authorities = authorities;
    }

    @Override
    public String getName() {
        return email;
    }

    @Override
    public boolean implies(Subject subject) {
        return false;
    }

    public static PlatformUser create(String email, List<GrantedAuthority> authorities) {
        if (StringUtils.isBlank(email)) throw new IllegalArgumentException("Email is blank: " + email);
        return new PlatformUser(email, authorities);
    }

    public Set<GrantedAuthority> getAuthorities() {
        UserRole role = this.getUserRole();
        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority(role.getRole().authority()));
        return authorities;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public UserRole getUserRole() {
        return new UserRole(id, Role.ADMIN);

    }

}

我在这里找到了答案:http://www.baeldung.com/jackson-field-serializable-deserializable-or-not

我在我的配置中添加了此 @Bean 以忽略 getters/setters 并仅使用属性:

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

    return mapper;
}