2021-08-02
This commit is contained in:
29
validation/README.md
Normal file
29
validation/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# spring-boot-validation-examples
|
||||
|
||||
```kotlin
|
||||
implementation ("org.springframework.boot:spring-boot-starter-validation")
|
||||
```
|
||||
|
||||
## Annotations
|
||||
|
||||
* @Digits
|
||||
* @Email
|
||||
* @Max
|
||||
* @Min
|
||||
* @Negative
|
||||
* @NotBlank
|
||||
* @NotEmpty
|
||||
* @NotNull
|
||||
* @Null
|
||||
* @Pattern
|
||||
* @Positive
|
||||
* @Size
|
||||
|
||||
https://docs.jboss.org/hibernate/beanvalidation/spec/2.0/api/
|
||||
|
||||
|
||||
|
||||
---
|
||||
developed by Elex
|
||||
|
||||
https://www.elex-project.com
|
||||
29
validation/build.gradle.kts
Normal file
29
validation/build.gradle.kts
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
plugins {
|
||||
id("elex-spring-boot")
|
||||
|
||||
id("org.springframework.boot") version "2.5.3"
|
||||
id("io.spring.dependency-management") version "1.0.11.RELEASE"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.springframework.boot:spring-boot-starter-mustache")
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
|
||||
implementation ("org.springframework.boot:spring-boot-starter-validation")
|
||||
|
||||
compileOnly("org.projectlombok:lombok")
|
||||
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
||||
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
|
||||
|
||||
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import kr.pe.elex.examples.model.Person;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class MyController {
|
||||
@GetMapping({"/"})
|
||||
public String index() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@PostMapping(value = {"/test1"})
|
||||
@ResponseBody
|
||||
public ResponseEntity<String> test1(@Valid @RequestBody Person person, BindingResult bindingResult) {
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
log.info("ERROR: {}", person);
|
||||
return ResponseEntity.badRequest().body("BAD");
|
||||
} else {
|
||||
log.info("OK: {}", person);
|
||||
return ResponseEntity.ok("GOOD");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = {"/test2"})
|
||||
@ResponseBody
|
||||
public ResponseEntity<String> test2(@Valid @RequestBody Person person) {
|
||||
|
||||
return ResponseEntity.ok("GOOD");
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class})
|
||||
@ResponseBody
|
||||
public Map<String, String> handleValidation(MethodArgumentNotValidException e) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
e.getBindingResult().getAllErrors().forEach(item -> {
|
||||
result.put(((FieldError) item).getField(),
|
||||
item.getDefaultMessage());
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class Person {
|
||||
@NotNull @NotEmpty
|
||||
@JsonProperty
|
||||
private String name;
|
||||
@Min(13)
|
||||
@JsonProperty
|
||||
private int age;
|
||||
@Email
|
||||
@JsonProperty
|
||||
private String email;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
5
validation/src/main/resources/application.yaml
Normal file
5
validation/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
spring:
|
||||
application:
|
||||
name: My spring-boot project
|
||||
server:
|
||||
port: 8080
|
||||
10
validation/src/main/resources/banner.txt
Normal file
10
validation/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
('-. ('-. ) (`-.
|
||||
_( OO) _( OO) ( OO ).
|
||||
(,------.,--. (,------.(_/. \_)-.
|
||||
| .---'| |.-') | .---' \ `.' /
|
||||
| | | | OO ) | | \ /\
|
||||
(| '--. | |`-' |(| '--. \ \ |
|
||||
| .--'(| '---.' | .--' .' \_)
|
||||
| `---.| | | `---. / .'. \
|
||||
`------'`------' `------''--' '--'
|
||||
powered by ELEX
|
||||
47
validation/src/main/resources/logback-spring.xml
Normal file
47
validation/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Spring-boot Examples
|
||||
~
|
||||
~ Copyright (c) 2020-2021. Elex. All Rights Reserved.
|
||||
~ https://www.elex-project.com/
|
||||
-->
|
||||
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
|
||||
|
||||
<springProperty name="LOG_DIR" source="logging.path"
|
||||
defaultValue="${user.home}/logs"/>
|
||||
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
|
||||
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<charset>UTF-8</charset>
|
||||
<pattern>${FILE_LOG_PATTERN}</pattern>
|
||||
</encoder>
|
||||
<file>${LOG_PATH}</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<logger name="kr.pe.elex" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="ROLLING-FILE"/>
|
||||
</logger>
|
||||
<root level="info">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="ROLLING-FILE"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
1
validation/src/main/resources/templates/home.mustache
Normal file
1
validation/src/main/resources/templates/home.mustache
Normal file
@@ -0,0 +1 @@
|
||||
<h1>Test</h1>
|
||||
Reference in New Issue
Block a user