有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在spring中只返回特定字段

在下面的代码中,当我使用projection时,它会返回Group的整个对象,但我只想获取类GrouproleName 我该怎么做

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... }

共 (1) 个答案

  1. # 1 楼答案

    首先我尝试了@RestResource(exported = false)它不起作用,但后来我尝试了 @JsonIgnore它终于起作用了: