【发布时间】:2018-10-28 18:20:08
【问题描述】:
假设我在前端有一个包含常用字段和下拉列表的表单。 在这些下拉菜单中,用户可以选择一个 option,并且每个 option 都链接到 Spring data JPA 中的一个 entity;
下拉菜单包含一些标签和对应实体的链接作为值。 然后,该值会在 POST 请求中传递到我们希望创建的实体的 PagingAndSorting 存储库。
假设它是具有用户名的用户,并且他必须与其中一个办公室(也是一个实体)相关联:
@Data
@Builder
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name="users")
public class User{
@Id
@Coluemn(name="USER_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@Column(name="USER_NAME", nullable=false)
private String userName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="OFFICE_ID", **nullable=false**)
private Office office;
}
我的第一个猜测是: 发送 POST 请求到http://localhost:8080/api/users/ contentType:'application/json'
{"userName":"Anton","office":"http://localhost:8080/api/offices/1"}
但是会抛出异常
{
"cause": {
"cause": null,
"message": "Cannot construct instance of `test.domain.Office` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/offices/1')\n at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 160] (through reference chain: test.domain.User[\"office\"])"
},
"message": "JSON parse error: Cannot construct instance of `test.domain.Office` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/offices/1'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `test.domain.Office` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/office/1')\n at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 160] (through reference chain: test.domain.User[\"office\"])"
}
我做错了什么?
【问题讨论】:
标签: ajax spring spring-mvc spring-data-jpa spring-rest