UserController.
java
@RestController
@RequestMapping("/users")
public class UserController {
// code
@PostMapping("/sign-up")
public void signUp(@RequestBody User user) {
//code
}
}
用户
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private long id;
@ManyToOne
@JoinColumn(name = "language_id")
private Language language;
// others
public User() {
}
}
所以,正如您所见,语言是一个独立的实体.但我希望能够发送以下JSON结构
{
"foreName" : "bla",
"sureName" : "blo",
"language" : "1"
}
但是我收到以下错误
Cannot construct instance of
entity.db.user.Language
(although at
least one Creator exists): no String-argument constructor/factory
method to deserialize from String value (‘1’);
我是否需要通过过滤器预先获取语言实体?是否有强制解析方法的表单?在这里正确地做到这一点的方法是什么?
最佳答案 在API方法中创建一个新的DTO对象Say UserDTO作为Request Body.处理DTO以形成用户实体,以进一步继续.