我有一个看起来像这样的 RestController
@RequestMapping(value = "/post", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> test(@RequestBody User user) {
System.out.println(user);
return ResponseEntity.ok(user);
}
Run Code Online (Sandbox Code Playgroud)
和看起来像这样的用户模型
class User {
@NotBlank
private String name;
private String city;
private String state;
}
Run Code Online (Sandbox Code Playgroud)
我有一个要求,用户可以在输入 JSON 中传递一些额外的附加属性,就像这样
{
"name": "abc",
"city": "xyz",
"state": "pqr",
"zip":"765234",
"country": "india"
}
Run Code Online (Sandbox Code Playgroud)
'zip' 和 'country' 是输入 JSON 中的附加属性。
在 Spring Boot 中有什么方法可以在 Request Body 中获得这些附加属性吗?
我知道一种方法,我可以使用“Map”或“JsonNode”或“HttpEntity”作为 Requestbody 参数。但是我不想使用这些类,因为我会丢失可以在“用户”模型对象中使用的 javax.validation。