Java 如何在spring中仅返回特定字段
在下面的代码中,当我使用projection时,它返回给我Group的整个对象,但我只想得到classJava 如何在spring中仅返回特定字段,java,json,spring,Java,Json,Spring,在下面的代码中,当我使用projection时,它返回给我Group的整个对象,但我只想得到classGroup的roleName 我该怎么做 Projection:UserWithProfile @Projection(name="UserWithProfile",types=User.class) public interface UserWithProfile extends UserGetters { UserPhoto getProfilePhoto(); } UserGet
Group的roleName
我该怎么做
Projection:UserWithProfile
@Projection(name="UserWithProfile",types=User.class)
public interface UserWithProfile extends UserGetters {
UserPhoto getProfilePhoto();
}
UserGetters
public interface UserGetters{
Long getId();
String getName();
String getLogonEmail();
boolean isEmailVerified();
Group getGroup();
}
@Entity
public class User implements Serializable, UserGetters {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, targetEntity = Property.class)
private Set<Property> favouriteProperty;
@OneToOne
private AcceptanceLetter acceptanceLetter;
@Column(unique=true)
private String logonEmail;
private String name;
@JsonProperty(access = Access.WRITE_ONLY)
@Column(updatable=false)
private String password;
private boolean emailVerified;
@ManyToOne
private Group group;
@OneToOne
private Business business;
private String address;
private String postcode;
private String phoneNo;
private String passportNo;
@Column(length=1000)
private String description;
@JsonIgnore
private float meanRating;
@OneToOne(mappedBy="user",targetEntity=UserPhoto.class)
private UserPhoto profilePhoto;
@ManyToOne
private Country country;
Getter and setters... }
User.class
public interface UserGetters{
Long getId();
String getName();
String getLogonEmail();
boolean isEmailVerified();
Group getGroup();
}
@Entity
public class User implements Serializable, UserGetters {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, targetEntity = Property.class)
private Set<Property> favouriteProperty;
@OneToOne
private AcceptanceLetter acceptanceLetter;
@Column(unique=true)
private String logonEmail;
private String name;
@JsonProperty(access = Access.WRITE_ONLY)
@Column(updatable=false)
private String password;
private boolean emailVerified;
@ManyToOne
private Group group;
@OneToOne
private Business business;
private String address;
private String postcode;
private String phoneNo;
private String passportNo;
@Column(length=1000)
private String description;
@JsonIgnore
private float meanRating;
@OneToOne(mappedBy="user",targetEntity=UserPhoto.class)
private UserPhoto profilePhoto;
@ManyToOne
private Country country;
Getter and setters... }
@实体
公共类用户实现可序列化的UserGetter{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@ManyToMany(cascade=CascadeType.REMOVE,fetch=FetchType.EAGER,targetEntity=Property.class)
私有财产;
@奥内托内
私人承兑信承兑信;
@列(唯一=真)
私人字符串logonEmail;
私有字符串名称;
@JsonProperty(access=access.WRITE_仅限)
@列(可更新=false)
私有字符串密码;
私有布尔值验证;
@许多酮
私人集团;
@奥内托内
私营企业;
私有字符串地址;
私人字符串邮政编码;
私有字符串phoneNo;
私人字符串护照号码;
@列(长度=1000)
私有字符串描述;
@杰索尼奥雷
私人浮动利率;
@OneTONE(mappedBy=“user”,targetEntity=UserPhoto.class)
私人用户照片档案照片;
@许多酮
私人国家;
Getter和setter…}
首先我尝试了@RestResource(exported=false)
它不起作用,但后来我尝试了
@JsonIgnore
它终于起作用了:'D使用Jackson的@JsonIgnore确实是一条路要走。见文件;