{"id":35159,"date":"2023-05-28T11:29:55","date_gmt":"2023-05-28T02:29:55","guid":{"rendered":"https:\/\/m9js.shop\/blog\/?p=35159"},"modified":"2023-05-28T11:29:55","modified_gmt":"2023-05-28T02:29:55","slug":"advanced-data-validation-strategies-with-spring-boot-and-hibernate-validator","status":"publish","type":"post","link":"https:\/\/m9js.shop\/blog\/development\/advanced-data-validation-strategies-with-spring-boot-and-hibernate-validator","title":{"rendered":"Advanced Data Validation Strategies with Spring Boot and Hibernate Validator"},"content":{"rendered":"

\uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc774\uc6a9\ud55c \uace0\uae09 \ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac \uc804\ub7b5<\/h1>\n

\ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac\ub294 \ub370\uc774\ud130\ub97c \uc0ac\uc6a9\ud558\uace0 \uc800\uc7a5\ud560 \ub54c \ub9e4\uc6b0 \uc911\uc694\ud569\ub2c8\ub2e4. \uc774\ub294 \ub370\uc774\ud130\uac00 \uc2e0\ub8b0\uc131 \uc788\uace0 \uc548\uc815\uc801\uc778\uc9c0 \ud655\uc778\ud558\ub294 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4. \uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc0ac\uc6a9\ud558\uc5ec \uace0\uae09 \ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac \uc804\ub7b5\uc744 \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \uc548\uc804\ud558\uace0 \uc2e0\ub8b0\uc131 \uc788\ub294 \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc744 \uac1c\ubc1c\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub780?<\/h3>\n

\uc2a4\ud504\ub9c1 \ubd80\ud2b8\ub294 \uc2a4\ud504\ub9c1 \ud504\ub808\uc784\uc6cc\ud06c\ub97c \uae30\ubc18\uc73c\ub85c \ud55c \ube60\ub974\uace0 \uac04\ub2e8\ud55c \uc6f9 \uc560\ud50c\ub9ac\ucf00\uc774\uc158 \uac1c\ubc1c\uc744 \uc704\ud55c \ud504\ub808\uc784\uc6cc\ud06c\uc785\ub2c8\ub2e4. \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub294 \uc790\ubc14 \uac1d\uccb4 \uc720\ud6a8\uc131 \uac80\uc0ac\ub97c \uc704\ud55c \uc624\ud508 \uc18c\uc2a4 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc785\ub2c8\ub2e4. \uc774 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub294 \uac1d\uccb4\uc5d0 \ub300\ud55c \uc81c\uc57d \uc870\uac74\uc744 \uc815\uc758\ud558\uace0 \uac1d\uccb4\uac00 \uc774\ub7ec\ud55c \uc81c\uc57d \uc870\uac74\uc744 \ucda9\uc871\ud558\ub294\uc9c0 \uac80\uc0ac\ud569\ub2c8\ub2e4.<\/p>\n

\uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \ud568\uaed8 \uc0ac\uc6a9\ud558\uba74 \ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac\ub97c \uc27d\uac8c \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub294 \uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc758 \uc790\ub3d9 \uc124\uc815 \uae30\ub2a5\uc744 \uc0ac\uc6a9\ud558\uc5ec \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uad6c\uc131\ud560 \uc218 \uc788\uae30 \ub54c\ubb38\uc785\ub2c8\ub2e4.<\/p>\n

\uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc774\uc6a9\ud55c \ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac<\/h3>\n

\uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc774\uc6a9\ud55c \ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \ubc29\uc2dd\uc73c\ub85c \uc774\ub8e8\uc5b4\uc9d1\ub2c8\ub2e4.<\/p>\n

    \n
  1. \uc5d4\ud2f0\ud2f0 \ud074\ub798\uc2a4\uc5d0 \uc81c\uc57d \uc870\uac74\uc744 \uc815\uc758\ud569\ub2c8\ub2e4.<\/li>\n
  2. \ucee8\ud2b8\ub864\ub7ec \ud074\ub798\uc2a4\uc5d0\uc11c \uc720\ud6a8\uc131 \uac80\uc0ac\ub97c \uc218\ud589\ud569\ub2c8\ub2e4.<\/li>\n
  3. \uc720\ud6a8\uc131 \uac80\uc0ac \uacb0\uacfc\uc5d0 \ub530\ub77c \ucc98\ub9ac\ub97c \uc218\ud589\ud569\ub2c8\ub2e4.<\/li>\n<\/ol>\n

    \ub2e4\uc74c\uc740 \uc5d4\ud2f0\ud2f0 \ud074\ub798\uc2a4\uc5d0\uc11c \uc81c\uc57d \uc870\uac74\uc744 \uc815\uc758\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n

    @Entity\npublic class User {\n\n    @Id\n    @GeneratedValue(strategy = GenerationType.AUTO)\n    private Long id;\n\n    @NotNull(message = \"Name cannot be null\")\n    private String name;\n\n    @Email(message = \"Email should be valid\")\n    private String email;\n\n    \/\/ getter and setter\n}<\/code><\/pre>\n

    \uc704 \uc608\uc81c\uc5d0\uc11c\ub294 @NotNull\uacfc @Email \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec name\uacfc email \ud544\ub4dc\uc5d0 \ub300\ud55c \uc81c\uc57d \uc870\uac74\uc744 \uc815\uc758\ud569\ub2c8\ub2e4. \uc774\uc81c \ucee8\ud2b8\ub864\ub7ec \ud074\ub798\uc2a4\uc5d0\uc11c \uc720\ud6a8\uc131 \uac80\uc0ac\ub97c \uc218\ud589\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

    @RestController\n@RequestMapping(\"\/users\")\npublic class UserController {\n\n    @Autowired\n    private UserService userService;\n\n    @PostMapping\n    public ResponseEntity createUser(@Valid @RequestBody User user) {\n        User savedUser = userService.save(user);\n        return ResponseEntity.ok(savedUser);\n    }\n\n    \/\/ other methods\n}<\/code><\/pre>\n

    \uc704 \uc608\uc81c\uc5d0\uc11c\ub294 @Valid \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec \uc720\ud6a8\uc131 \uac80\uc0ac\ub97c \uc218\ud589\ud569\ub2c8\ub2e4. \ub9cc\uc57d \uac80\uc0ac \uacb0\uacfc\uc5d0 \uc774\uc0c1\uc774 \uc788\uc73c\uba74 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4. \uc774\ub97c \ucc98\ub9ac\ud558\uae30 \uc704\ud574 \ub2e4\uc74c\uacfc \uac19\uc774 \uc608\uc678 \ucc98\ub9ac\ub97c \uc218\ud589\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

    @ControllerAdvice\npublic class ExceptionHandlerController {\n\n    @ExceptionHandler(MethodArgumentNotValidException.class)\n    public ResponseEntity handleValidationExceptions(\n      MethodArgumentNotValidException ex) {\n        List errors = ex.getBindingResult()\n          .getAllErrors()\n          .stream()\n          .map(DefaultMessageSourceResolvable::getDefaultMessage)\n          .collect(Collectors.toList());\n\n        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);\n    }\n}<\/code><\/pre>\n

    \uc704 \uc608\uc81c\uc5d0\uc11c\ub294 @ExceptionHandler \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec MethodArgumentNotValidException \uc608\uc678\ub97c \ucc98\ub9ac\ud569\ub2c8\ub2e4. \uc774 \uc608\uc678\ub294 \uc720\ud6a8\uc131 \uac80\uc0ac\uc5d0 \uc2e4\ud328\ud588\uc744 \ub54c \ubc1c\uc0dd\ud569\ub2c8\ub2e4. \uc5d0\ub7ec \uba54\uc2dc\uc9c0\ub97c \ucd94\ucd9c\ud558\uc5ec ResponseEntity\ub85c \ubc18\ud658\ud569\ub2c8\ub2e4.<\/p>\n

    \uc720\ud6a8\uc131 \uac80\uc0ac \ud655\uc7a5: \ucee4\uc2a4\ud140 \ubc38\ub9ac\ub370\uc774\ud130 \uc81c\uc791\uacfc \ud65c\uc6a9<\/h1>\n

    \ud558\uc774\ubc84\ub124\uc774\ud2b8 \ubc38\ub9ac\ub370\uc774\ud130\ub294 \ub2e4\uc591\ud55c \uc81c\uc57d \uc870\uac74\uc744 \uc81c\uacf5\ud569\ub2c8\ub2e4. \ud558\uc9c0\ub9cc \ub54c\ub85c\ub294 \uc0ac\uc6a9\uc790\uac00 \uc9c1\uc811 \uc81c\uc57d \uc870\uac74\uc744 \uc815\uc758\ud574\uc57c \ud560 \ub54c\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub97c \uc704\ud574 \ucee4\uc2a4\ud140 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc81c\uc791\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

    \ucee4\uc2a4\ud140 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc81c\uc791\ud558\ub824\uba74 \ub2e4\uc74c\uacfc \uac19\uc740 \ubc29\ubc95\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

      \n
    1. ConstraintValidator \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud569\ub2c8\ub2e4.<\/li>\n
    2. \uc81c\uc57d \uc870\uac74 \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc815\uc758\ud569\ub2c8\ub2e4.<\/li>\n
    3. \uc5b4\ub178\ud14c\uc774\uc158\uc5d0 ConstraintValidator\ub97c \uc801\uc6a9\ud569\ub2c8\ub2e4.<\/li>\n<\/ol>\n

      \ub2e4\uc74c\uc740 \ucee4\uc2a4\ud140 \ubc38\ub9ac\ub370\uc774\ud130\ub97c \uc81c\uc791\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n

      \n@Target({ FIELD })\n@Retention(RUNTIME)\n@Constraint(validatedBy = CheckPhoneNumberValidator.class)\n@Documented\npublic @interface CheckPhoneNumber {\n    String message() default \"Invalid phone number\";\n    Class[] groups() default {};\n    Class<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

      \uc2a4\ud504\ub9c1 \ubd80\ud2b8\uc640 Hibernate Validator\ub97c \uc774\uc6a9\ud55c \uace0\uae09 \ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac \uc804\ub7b5\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf4\uc790.<\/p>\n","protected":false},"author":1,"featured_media":12882,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[5898,2104,2105,2149,2188,2156,5932,2051],"class_list":["post-35159","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-backend","tag-data","tag-java","tag-methods","tag-public","tag-spring","tag-spring-boot","tag-strategies"],"acf":[],"_links":{"self":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/35159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/comments?post=35159"}],"version-history":[{"count":1,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/35159\/revisions"}],"predecessor-version":[{"id":35166,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/35159\/revisions\/35166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media\/12882"}],"wp:attachment":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media?parent=35159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/categories?post=35159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/tags?post=35159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}