우선, @Valid
을 사용하고 json 문자열 대신에 User
인스턴스를 사용하는 경우 사용자를 유효하게 할 수 있습니다. 즉 좋은 예 : 작동 :주석 유효성 검사
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(@Valid @ModelAttribute("user") User user, BindingResult result) {
userRep.save(user);
return "redirect:/";
}
그래서 질문은, @Email
, @NotEmpty
예를 들어, 자동으로 주석을 내가 API의 어떤 종류를 생성하고 수신이 JSON 문자열이며, 검증 할 수있는 방법인가? 가장 좋은 방법은 무엇입니까?
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> createFromJson(@RequestBody String json) {
User user = User.fromJsonToUser(json);
return new ResponseEntity<String>(user.toJson(),header,HttpStatus.CREATED);
}
User.class
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty
@Length(max = 30)
private String firstName;
@Email
@NotEmpty
@Length(min = 3 , max = 50)
private String primaryEmail;
}
'@ RequestBody' 주석 매개 변수에'@Va 뚜껑 '도. –
'@ Valid'는 json 문자열입니까? ('@ RequestBody''@ Valid' String json)하지만 사용자 객체의 유효성을 검사하고 싶습니다. – Jaxox