Java 在SpringREST应用程序中获取用户角色的问题

Java 在SpringREST应用程序中获取用户角色的问题,java,spring,rest,roles,Java,Spring,Rest,Roles,我有一个RestController类,包含以下内容: @RestController public class UserRestController { @Autowired UserService userService; @Autowired private SecurityService securityService; @Autowired private UserValidator userValidator; // Get a Single User @GetMapping

我有一个RestController类,包含以下内容:

@RestController
public class UserRestController 
{
@Autowired
UserService userService;

@Autowired
private SecurityService securityService;

@Autowired
private UserValidator userValidator;

// Get a Single User
@GetMapping("/api/users/{id}")
public User getUserById(@PathVariable(value = "id") Long userId) {
    return userService.getUserById(userId);
}
这是UserService中的getUserById函数:

 public User getUserById(@PathVariable(value = "id") Long userId) {
    return userRepository.findById(userId).orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
}
这是本地主机8080/api/users/11上的GET请求的结果:

{
"id": 11,
"name": null,
"email": null,
"password": "$2a$10$HykDWcHU3vO9YAcdXiWieua9YyYMkwrNIk7WgpmVzVwENb71fDCsW",
"status": null,
"tel": null,
"confirmation": null,
"birth_date": null,
"createdAt": "2018-05-22T09:09:12.000+0000",
"updatedAt": "2018-05-22T09:09:12.000+0000",
"username": "ouissal@gmail.com"
}
这是本地主机8080/users/11上的GET请求的结果

{
"name": null,
"email": null,
"password": "$2a$10$HykDWcHU3vO9YAcdXiWieua9YyYMkwrNIk7WgpmVzVwENb71fDCsW",
"status": null,
"tel": null,
"confirmation": null,
"birth_date": null,
"createdAt": "2018-05-22T09:09:12.000+0000",
"updatedAt": "2018-05-22T09:09:12.000+0000",
"username": "ouissal@gmail.com",
"_links": {
    "self": {
        "href": "http://localhost:8080/users/11"
    },
    "user": {
        "href": "http://localhost:8080/users/11"
    },
    "roles": {
        "href": "http://localhost:8080/users/11/roles"
    }
}
}
我的控制器中没有为/用户映射的任何内容,如何使用我的控制器获取角色


编辑 这是我的用户类:

@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
    allowGetters = true)
公共类用户实现可序列化{

private static final long serialVersionUID = 1L;


public User() {
    super();
    // TODO Auto-generated constructor stub
}

@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "user_name")
//@NotBlank
private String name;

@Column(name = "user_email")
//@NotBlank
private String email;

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

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

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

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

@Column(name = "user_birth_date")
@Temporal(TemporalType.DATE)
private Date birth_date;

@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;

@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;

@JsonBackReference
@ManyToMany
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

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

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getTel() {
    return tel;
}

public void setTel(String tel) {
    this.tel = tel;
}

public String getConfirmation() {
    return confirmation;
}

public void setConfirmation(String confirmation) {
    this.confirmation = confirmation;
}

public Date getBirth_date() {
    return birth_date;
}

public void setBirth_date(Date birth_date) {
    this.birth_date = birth_date;
}

public Date getCreatedAt() {
    return createdAt;
}

public Date getUpdatedAt() {
    return updatedAt;
}

public void setUpdatedAt(Date updatedAt) {
    this.updatedAt = updatedAt;
}

public Set<Role> getRoles() {
    return roles;
}

public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
}
private static final long serialVersionUID=1L;
公共用户(){
超级();
//TODO自动生成的构造函数存根
}
@身份证
@列(name=“user\u id”)
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@列(name=“user\u name”)
//@不空白
私有字符串名称;
@列(name=“user\u email”)
//@不空白
私人字符串电子邮件;
@列(name=“user\u password”)
@不空白
私有字符串密码;
@列(name=“user\u status”)
私有字符串状态;
@列(name=“user\u tel”)
私人电话;
@列(name=“用户确认”)
私有字符串确认;
@列(name=“user\u birth\u date”)
@时态(TemporalType.DATE)
私人日期出生日期;
@列(nullable=false,Updateable=false)
@时态(TemporalType.TIMESTAMP)
@创建数据
私人日期创建日期;
@列(nullable=false)
@时态(TemporalType.TIMESTAMP)
@最后修改日期
私人日期更新日期;
@JsonBackReference
@许多
@JoinTable(name=“user\u role”,joinColumns=@JoinColumn(name=“user\u id”),inverseJoinColumns=@JoinColumn(name=“role\u id”))
私人设定角色;
@列(name=“username”)
@不空白
私有字符串用户名;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共字符串getTel(){
返回电话;
}
公共电话(字符串电话){
this.tel=tel;
}
公共字符串getConfirmation(){
返回确认;
}
公共无效集合确认(字符串确认){
这个。确认=确认;
}
公开日期出生日期{
返回出生日期;
}
公共无效设置出生日期(出生日期){
this.birth\u date=出生日期;
}
公共日期getCreatedAt(){
返回createdAt;
}
公共日期getUpdatedAt(){
返回更新数据;
}
公共无效设置日期日期(日期更新日期){
this.updatedAt=updatedAt;
}
公共集getRoles(){
返回角色;
}
公共无效集合角色(集合角色){
this.roles=角色;
}
}
这是我的角色课:

@Entity
@Table(name = "role")
public class Role {

@Id
@Column(name = "role_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "role_name")
private String name;
@ManyToMany(mappedBy = "roles")
@JsonManagedReference
private Set<User> users;



public Role() {
    super();
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public Set<User> getUsers() {
    return users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}
}
@实体
@表(name=“role”)
公共阶级角色{
@身份证
@列(name=“role\u id”)
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@列(name=“role\u name”)
私有字符串名称;
@许多(mappedBy=“角色”)
@JsonManagedReference
私人用户;
公共角色(){
超级();
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共集getUsers(){
返回用户;
}
公共void集合用户(集合用户){
this.users=用户;
}
}

带有
JsonBackReference
注释的属性将不包含在序列化内容中。 要包含角色,请在
角色
用户
中交换
JsonBackReference
JsonManagedReference
注释

这将包括
角色
用户
中,但不是反过来


有关更多信息,请查看此

是否尝试点击
角色
url
http://localhost:8080/users/11/roles
您在那里得到角色了吗?是的,我得到了角色,但我不明白的是,尽管我的控制器不是为/users映射的,而是为/api/users映射的,但为什么我要得到角色。我也不知道我应该用什么方式编辑我的控制器,让它们位于/api/users/你能提供
User
类的代码吗?您直接将其作为响应返回,因此如果您想在json响应中看到角色,它还必须包含角色。您是否使用
spring data rest
lib?看起来您正在学习本教程:看看这个答案:,我想这可以解释为什么您的角色没有序列化为json