{"id":49888,"date":"2024-08-03T13:05:31","date_gmt":"2024-08-03T04:05:31","guid":{"rendered":"https:\/\/8gfg.shop\/blog\/development\/take-advantage-of-advanced-data-validation-and-bean-validation-in-spring-boot"},"modified":"2024-08-03T13:44:48","modified_gmt":"2024-08-03T04:44:48","slug":"take-advantage-of-advanced-data-validation-and-bean-validation-in-spring-boot","status":"publish","type":"post","link":"https:\/\/8gfg.shop\/blog\/development\/take-advantage-of-advanced-data-validation-and-bean-validation-in-spring-boot","title":{"rendered":"Spring Boot\uc5d0\uc11c\uc758 \uace0\uae09 \ub370\uc774\ud130 \uac80\uc99d \ubc0f Bean Validation \ud65c\uc6a9"},"content":{"rendered":"
Spring Boot\ub294 Java \uae30\ubc18\uc758 \uc560\ud50c\ub9ac\ucf00\uc774\uc158 \uac1c\ubc1c\uc744 \uac04\uc18c\ud654\ud558\ub294 \ud504\ub808\uc784\uc6cc\ud06c\ub85c, \ub370\uc774\ud130 \uac80\uc99d \ubc0f \uc720\ud6a8\uc131 \uac80\uc0ac\uc5d0 \ub300\ud55c \uac15\ub825\ud55c \uc9c0\uc6d0\uc744 \uc81c\uacf5\ud569\ub2c8\ub2e4. \uc774 \uae00\uc5d0\uc11c\ub294 Spring Boot\uc5d0\uc11c \uace0\uae09 \ub370\uc774\ud130 \uac80\uc99d\uc744 \uad6c\ud604\ud558\ub294 \ubc29\ubc95\uacfc Bean Validation\uc744 \ud65c\uc6a9\ud558\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574 \ub2e4\ub8e8\uaca0\uc2b5\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \uac1c\ubc1c\uc790\ub294 \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc758 \ub370\uc774\ud130 \ubb34\uacb0\uc131\uc744 \ubcf4\uc7a5\ud558\uace0, \uc0ac\uc6a9\uc790 \uacbd\ud5d8\uc744 \ud5a5\uc0c1\uc2dc\ud0ac \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n
\ub370\uc774\ud130 \uac80\uc99d\uc740 \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc758 \uc2e0\ub8b0\uc131\uacfc \uc548\uc815\uc131\uc744 \ub192\uc774\ub294 \ub370 \ud544\uc218\uc801\uc785\ub2c8\ub2e4. \uc798\ubabb\ub41c \ub370\uc774\ud130\uac00 \uc2dc\uc2a4\ud15c\uc5d0 \uc785\ub825\ub418\uba74 \ub2e4\uc74c\uacfc \uac19\uc740 \ubb38\uc81c\uac00 \ubc1c\uc0dd\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4:<\/p>\n
\ub530\ub77c\uc11c, \ub370\uc774\ud130 \uac80\uc99d\uc740 \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc758 \uc804\ubc18\uc801\uc778 \ud488\uc9c8\uc744 \ub192\uc774\ub294 \ub370 \uc911\uc694\ud55c \uc5ed\ud560\uc744 \ud569\ub2c8\ub2e4.<\/p>\n
Bean Validation\uc740 Java EE\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 \ud45c\uc900\uc73c\ub85c, \uac1d\uccb4\uc758 \uc18d\uc131\uc5d0 \ub300\ud55c \uc720\ud6a8\uc131 \uac80\uc0ac\ub97c \uc218\ud589\ud558\ub294 API\uc785\ub2c8\ub2e4. Spring Boot\uc5d0\uc11c\ub294 Hibernate Validator\ub97c \uae30\ubcf8 \uad6c\ud604\uccb4\ub85c \uc0ac\uc6a9\ud569\ub2c8\ub2e4. Bean Validation\uc744 \uc0ac\uc6a9\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\uc740 \uc774\uc810\uc744 \ub204\ub9b4 \uc218 \uc788\uc2b5\ub2c8\ub2e4:<\/p>\n
\ub2e4\uc74c\uc740 Bean Validation\uc744 \uc0ac\uc6a9\ud558\ub294 \uac04\ub2e8\ud55c \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n
import javax.validation.constraints.NotBlank;\nimport javax.validation.constraints.Size;\n\npublic class User {\n\n @NotBlank(message = \"\uc774\ub984\uc740 \ud544\uc218\uc785\ub2c8\ub2e4.\")\n private String name;\n\n @Size(min = 8, message = \"\ube44\ubc00\ubc88\ud638\ub294 \ucd5c\uc18c 8\uc790 \uc774\uc0c1\uc774\uc5b4\uc57c \ud569\ub2c8\ub2e4.\")\n private String password;\n\n \/\/ getters and setters\n}<\/code><\/pre>\n\uc704\uc758 \uc608\uc81c\uc5d0\uc11c @NotBlank<\/code>\uc640 @Size<\/code> \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec User<\/code> \ud074\ub798\uc2a4\uc758 \uc18d\uc131\uc744 \uac80\uc99d\ud558\uace0 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n3. Spring Boot\uc5d0\uc11c\uc758 \ub370\uc774\ud130 \uac80\uc99d \uc124\uc815<\/h2>\n
Spring Boot\uc5d0\uc11c Bean Validation\uc744 \uc124\uc815\ud558\ub294 \uac83\uc740 \ub9e4\uc6b0 \uac04\ub2e8\ud569\ub2c8\ub2e4. \uae30\ubcf8\uc801\uc73c\ub85c Spring Boot\ub294 Hibernate Validator\ub97c \ud3ec\ud568\ud558\uace0 \uc788\uc73c\uba70, \ucd94\uac00\uc801\uc778 \uc124\uc815 \uc5c6\uc774\ub3c4 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uadf8\ub7ec\ub098 \ud2b9\uc815 \uc694\uad6c \uc0ac\ud56d\uc5d0 \ub530\ub77c \uc124\uc815\uc744 \uc870\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n
\uc124\uc815 \ubc29\ubc95<\/h3>\n\n- \n
\uc758\uc874\uc131 \ucd94\uac00<\/strong>: Maven \ub610\ub294 Gradle\uc744 \uc0ac\uc6a9\ud558\uc5ec Hibernate Validator \uc758\uc874\uc131\uc744 \ucd94\uac00\ud569\ub2c8\ub2e4.<\/p>\n\n org.hibernate.validator\n hibernate-validator\n 6.2.0.Final\n<\/code><\/pre>\n<\/li>\n- \n
\uac80\uc99d \uadf8\ub8f9 \uc124\uc815<\/strong>: \ud2b9\uc815 \uc0c1\ud669\uc5d0 \ub530\ub77c \uac80\uc99d \uadf8\ub8f9\uc744 \uc124\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4, \ud68c\uc6d0\uac00\uc785\uacfc \ub85c\uadf8\uc778 \uc2dc \uc11c\ub85c \ub2e4\ub978 \uac80\uc99d \uaddc\uce59\uc744 \uc801\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<\/li>\n- \n
Global Exception Handling<\/strong>: \uac80\uc99d \uc2e4\ud328 \uc2dc \ubc1c\uc0dd\ud558\ub294 \uc608\uc678\ub97c \ucc98\ub9ac\ud558\uae30 \uc704\ud574 \uae00\ub85c\ubc8c \uc608\uc678 \ucc98\ub9ac\uae30\ub97c \uc124\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<\/li>\n<\/ol>\nimport org.springframework.http.HttpStatus;\nimport org.springframework.web.bind.annotation.ControllerAdvice;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\nimport org.springframework.web.bind.annotation.ResponseStatus;\n\n@ControllerAdvice\npublic class GlobalExceptionHandler {\n\n @ExceptionHandler(MethodArgumentNotValidException.class)\n @ResponseStatus(HttpStatus.BAD_REQUEST)\n public ResponseEntity handleValidationExceptions(MethodArgumentNotValidException ex) {\n return new ResponseEntity(ex.getBindingResult().getAllErrors().get(0).getDefaultMessage(), HttpStatus.BAD_REQUEST);\n }\n}<\/code><\/pre>\n4. \ucee4\uc2a4\ud140 \uac80\uc99d \ub85c\uc9c1 \uad6c\ud604<\/h2>\n
Bean Validation\uc758 \uac15\ub825\ud55c \uae30\ub2a5 \uc911 \ud558\ub098\ub294 \ucee4\uc2a4\ud140 \uac80\uc99d \ub85c\uc9c1\uc744 \uad6c\ud604\ud560 \uc218 \uc788\ub2e4\ub294 \uc810\uc785\ub2c8\ub2e4. \ud2b9\uc815 \ube44\uc988\ub2c8\uc2a4 \uc694\uad6c \uc0ac\ud56d\uc5d0 \ub9de\ub294 \uac80\uc99d \uaddc\uce59\uc774 \ud544\uc694\ud560 \ub54c \uc720\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n
\ucee4\uc2a4\ud140 \uc5b4\ub178\ud14c\uc774\uc158 \uc0dd\uc131<\/h3>\n\n- \uc5b4\ub178\ud14c\uc774\uc158 \uc815\uc758<\/strong>:<\/li>\n<\/ol>\n
import javax.validation.Constraint;\nimport javax.validation.Payload;\nimport java.lang.annotation.ElementType;\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.RetentionPolicy;\nimport java.lang.annotation.Target;\n\n@Constraint(validatedBy = CustomValidator.class)\n@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })\n@Retention(RetentionPolicy.RUNTIME)\npublic @interface ValidCustom {\n String message() default \"\uc720\ud6a8\ud558\uc9c0 \uc54a\uc740 \uac12\uc785\ub2c8\ub2e4.\";\n Class[] groups() default {};\n Class[] payload() default {};\n}<\/code><\/pre>\n\n- \uac80\uc99d \ub85c\uc9c1 \uad6c\ud604<\/strong>:<\/li>\n<\/ol>\n
import javax.validation.ConstraintValidator;\nimport javax.validation.ConstraintValidatorContext;\n\npublic class CustomValidator implements ConstraintValidator {\n\n @Override\n public boolean isValid(String value, ConstraintValidatorContext context) {\n \/\/ \ucee4\uc2a4\ud140 \uac80\uc99d \ub85c\uc9c1\n return value != null && value.matches(\"^[a-zA-Z0-9]*$\");\n }\n}<\/code><\/pre>\n\uc774\uc81c @ValidCustom<\/code> \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec \ud2b9\uc815 \ud544\ub4dc\uc5d0 \ub300\ud55c \ucee4\uc2a4\ud140 \uac80\uc99d\uc744 \uc801\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n5. \uacb0\ub860 \ubc0f \uc694\uc57d<\/h2>\n
Spring Boot\uc5d0\uc11c \uace0\uae09 \ub370\uc774\ud130 \uac80\uc99d \ubc0f Bean Validation\uc744 \ud65c\uc6a9\ud558\ub294 \uac83\uc740 \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc758 \ud488\uc9c8\uacfc \uc2e0\ub8b0\uc131\uc744 \ub192\uc774\ub294 \ub370 \ub9e4\uc6b0 \uc911\uc694\ud569\ub2c8\ub2e4. \ub370\uc774\ud130 \uac80\uc99d\uc740 \ubcf4\uc548, \ube44\uc988\ub2c8\uc2a4 \ub85c\uc9c1, \uc0ac\uc6a9\uc790 \uacbd\ud5d8 \ub4f1 \uc5ec\ub7ec \uce21\uba74\uc5d0\uc11c \uae0d\uc815\uc801\uc778 \uc601\ud5a5\uc744 \ubbf8\uce69\ub2c8\ub2e4. Bean Validation\uc740 \uac04\ub2e8\ud55c \uc5b4\ub178\ud14c\uc774\uc158 \uae30\ubc18\uc758 \uc811\uadfc \ubc29\uc2dd\uc744 \uc81c\uacf5\ud558\uba70, \ud544\uc694\uc5d0 \ub530\ub77c \ucee4\uc2a4\ud140 \uac80\uc99d \ub85c\uc9c1\uc744 \uad6c\ud604\ud560 \uc218 \uc788\ub294 \uc720\uc5f0\uc131\uc744 \uc81c\uacf5\ud569\ub2c8\ub2e4.<\/p>\n
\uc774 \uae00\uc5d0\uc11c \ub2e4\ub8ec \ub0b4\uc6a9\uc744 \uc694\uc57d\ud558\uc790\uba74:<\/p>\n
\n- \ub370\uc774\ud130 \uac80\uc99d\uc758 \uc911\uc694\uc131\uacfc \uadf8\ub85c \uc778\ud55c \uc774\uc810<\/li>\n
- Bean Validation\uc758 \uae30\ubcf8 \uac1c\ub150\uacfc \uc124\uc815 \ubc29\ubc95<\/li>\n
- \ucee4\uc2a4\ud140 \uac80\uc99d \ub85c\uc9c1 \uad6c\ud604 \ubc29\ubc95<\/li>\n<\/ul>\n
\uc774\ub7ec\ud55c \uc9c0\uc2dd\uc744 \ubc14\ud0d5\uc73c\ub85c Spring Boot \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc5d0\uc11c \ub370\uc774\ud130 \uac80\uc99d\uc744 \ud6a8\uacfc\uc801\uc73c\ub85c \uad6c\ud604\ud558\uc5ec \ub354 \ub098\uc740 \uc0ac\uc6a9\uc790 \uacbd\ud5d8\uacfc \uc2dc\uc2a4\ud15c \uc548\uc815\uc131\uc744 \uc81c\uacf5\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"
Spring Boot\uc5d0\uc11c\uc758 \uace0\uae09 \ub370\uc774\ud130 \uac80\uc99d \ubc0f Bean Validation \ud65c\uc6a9 Spring Boot\ub294 Java \uae30\ubc18\uc758 \uc560\ud50c\ub9ac\ucf00\uc774\uc158 \uac1c\ubc1c\uc744 […]<\/p>\n","protected":false},"author":1,"featured_media":33704,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[2119,584,2105],"class_list":["post-49888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-api","tag-global","tag-java"],"acf":[],"_links":{"self":[{"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/posts\/49888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/comments?post=49888"}],"version-history":[{"count":1,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/posts\/49888\/revisions"}],"predecessor-version":[{"id":49907,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/posts\/49888\/revisions\/49907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/media\/33704"}],"wp:attachment":[{"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/media?parent=49888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/categories?post=49888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/8gfg.shop\/blog\/wp-json\/wp\/v2\/tags?post=49888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}