0

私は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...
}
4

2 に答える 2

1

json が無効です。次のように変更してください。

{
    "name": "John",
    "role": "admin"
}
于 2015-12-04T10:36:58.193 に答える