[英]public property overshadowed by public getter in json serialization
我有一个奇怪的情况-我有一个PlatformUser
类,该类实现了返回其email
Principal
getName()
。 授权需要此权限。 我希望能够基于公共属性name
和email
对PlatformUser
进行序列化和反序列化。 我应该如何注释我的类以使其起作用。作为一种变通办法,我不得不将属性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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.