私はUser
オブジェクトを持っていRole
ます。各ユーザーには 1 つの役割があります。データベースでは、ロールはテーブルへの外部キーroles
であり、各ロールは主キーとして数値 ID と、ロールのテキスト名 (「admin」、「user」) のみを持ちます。
POST
ここで、次の JSONを単純化できるようにしたいと考えています。
{"name": "John", "role": "admin"}
どうやってするか?
私はこのエラーで終わります:
Could not read document: Can not instantiate value of type [simple type, class Role] from String value ('admin'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@7b8a088a; line: 1, column: 17] (through reference chain: User[\"role\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, Role] from String value ('admin'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@7b8a088a; line: 1, column: 17] (through reference chain: User[\"role\"])
ユーザーモデル:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull
private String name;
@NotNull
@ManyToOne
private Role role;
// Getters and setters...
}
ロールモデル:
@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull
private String name;
// Getters and setters...
}