Spring Boot React 超媒体字符串 id

Spingboot React Hypermedia string id

我正在使用 SpringBoot、Hibernate 和 React 构建一个 Web 应用程序。使用 spring 教程创建具有自动生成的 ID 的 'normal' 对象时,我没有遇到任何问题。对于我的用户 table,我不太认为需要一个自动增量 ID,这将需要一个单独的索引,所以我试图将用户名设为主键。 (这也将使所有引用更容易破译)。这是我的用户模型:

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;

@Data
@Entity
@Table(name="user")
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3924062441897826838L;
    @Id
    @Column(name="username")
    private String userName;
    @Column(name="password")
    private String password;
    @Column(name="role")
    private String role;
    @Column(name="first_name")
    private String firstName;
    @Column(name="last_name")
    private String lastName;
    @Column(name="country")
    private String country;
    @Column(name="enabled")
    private boolean enabled;
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            },
            mappedBy = "users")
    private Set<Project> projects = new HashSet<>();
    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "createUser")
    private Set<Project> createdProjects = new HashSet<>();

    private User() {    }
    public User(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "User [userName="+ userName+",firstName="+firstName+"]";
    }


}

我似乎无法通过基本路由访问我的主键(用户名)。 (this.props.user.userName...在反应中)。我知道它嵌入在 _links 中,但是我的列表页面有什么方法可以访问它吗?

**
 * User Object
 */
class User extends React.Component {

    constructor(props) {
        super(props);
        this.handleDelete = this.handleDelete.bind(this);
    }

    handleDelete() {
        this.props.onDelete(this.props.user);
    }

    render() {
        return (
          <tr>
            <td>**{this.props.user.userName} is BLANK**</td>
            <td>{this.props.user.firstName}</td>
            <td>{this.props.user.lastName}</td>
            <td>{this.props.user.enabled}</td>
            <td>
            <button onClick={this.handleDelete}>Delete</button>
        </td>
          </tr>);
      }
}

给User添加自增id是唯一的方法吗?非常感谢所有帮助。谢谢!

更新:这是 console.log(this.props.user) 的输出:

    Object

_links: Object

createdProjects: {href: "http://localhost:8080/api/users/testUserName/createdProjects"}

projects: {href: "http://localhost:8080/api/users/testUserName/projects"}

self: {href: "http://localhost:8080/api/users/testUserName"}

user: {href: "http://localhost:8080/api/users/testUserName"}

Object Prototype

country: "TestCountry"

enabled: true

firstName: "TestName"

lastName: "TestLastName"

password: "testPword"

role: "testrole"
默认情况下,

Spring 不会公开实体 class 的 ID。您可以将其配置为公开某个实体的 ID class。下面的代码应该可以完成工作。

public class RestConfiguration extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(User.class);
    }
}