在Spring Rest应用程序中获取用户角色的问题

时间:2018-05-28 08:45:05

标签: 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("/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));
}

这是 localhost: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"
}

这是 localhost: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"
    }
}
}

我的控制器中没有为/ users映射的任何内容,如何使用我的控制器获取角色?

修改

这是我的用户类:

@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
    allowGetters = true)

public class User实现Serializable {

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

这是我的角色类:

@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;
}
}

1 个答案:

答案 0 :(得分:0)

使用JsonBackReference注释的属性不会包含在序列化内容中。 要包含角色,请在JsonBackReferenceJsonManagedReference中交换RoleUser注释。

这会将Roles包含在User中,但不包括其他方式。

有关详细信息,请查看此answer