This project is a set of validation or responses commonly used among Spring projects.
The following dependencies are required for use.
์ด ํ๋ก์ ํธ๋ Spring ํ๋ก์ ํธ์์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ ํจ์ฑ ๊ฒ์ฌ ๋๋ ์๋ต์ ์งํฉ์ ๋๋ค.
์ฌ์ฉ์์๋ ์๋์ ๊ฐ์ ์์กด์ฑ์ด ์๊ตฌ๋ฉ๋๋ค.
If you are using spring MVC
org.springframework.srping-web
javax.validation.validation-api
for example, pom.xml
<dependencies>
...
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
...
</dependencies>
OR using spring-boot
spring-boot-starter-web
spring-boot-starter-validation
for example, build.gradle
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
๐ฅ After adding all of the above dependencies, register our dependencies! ๐ฅ
์์ ์์กด์ฑ์ ๋ชจ๋ ์ถ๊ฐํ ํ, ์ฐ๋ฆฌ์ ์์กด์ฑ์ ๋ฑ๋กํด์ฃผ์ธ์!
In pom.xml
<dependency>
<groupId>io.github.doohee94</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
In build.gradle
implementation 'io.github.doohee94:common:1.0'
Currently, the following features are included. Many features will be added in the future. ๐
ํ์ฌ๋ ์๋์ ๊ฐ์ ๊ธฐ๋ฅ๋ค์ด ํฌํจ๋์ด์์ต๋๋ค. ์์ผ๋ก ๋ง์ ๊ธฐ๋ฅ๋ค์ด ์ถ๊ฐ๋ ์์ ์ ๋๋ค.
-
File
-
This validation can validate the request of MultipartFile.
-
You can check null values and check extensions.
-
Please refer to the code below for how to use it
FileDto.java
public class FileDto { @File(acceptExtensions = {"png","jpg"}, nullable = true) private MultipartFile file; }
Controller.java
@RestController public class Controller { @PostMapping public void test(@Valid FileDto fileDto){ //code.. } }
If you upload a file with an extension other than 'png' or 'jpg', an error will occur.
You can also receive null values through nullable. But, Remember that it defaults to 'false'.
์ด validation์ MultipartFile์ Request์ ์ ํจ์ฑ์ ๊ฒ์ฌํฉ๋๋ค.
๋น์ ์ null์ ์ฒดํฌํ ์์๊ณ , ํ์ผ์ ํ์ฅ์๋ฅผ ์ฒดํฌํ ์ ์์ต๋๋ค.
๋ง์ฝ, ๋น์ ์ด ํ์ผ์ 'png'๋ 'jpg'๊ฐ ์๋ ๋ค๋ฅธ ํ์ฅ์ ํ์ผ์ ์ ๋ก๋ํ ๊ฒฝ์ฐ, ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋ ๊ฒ์ ๋๋ค.
nullable์ ํตํด null๊ฐ์ ๋ฐ์ ์๋ ์์ต๋๋ค. ํ์ง๋ง ๊ทธ๊ฒ์ ๊ธฐ๋ณธ๊ฐ์ 'false'์์ ๊ธฐ์ตํ์ญ์์ค.
-
-
Enum
-
This validation can validate the request of Enum
-
You can check null values and check value.
-
Please refer to the code below for how to use it.
Gender.java
public enum Gender{ MALE, FEMALE ; }
SampleDto.java
public class SampleDto { @CheckEnum(enumClass = Gender.class, message = "Please Check Gender", nullable = true) private String gender; }
Controller.java
@RestController public class Controller { @GetMapping public void test(@Valid SampleDto sample){ //code.. } }
If you enter a value other than 'MALE' and 'FEMALE' in the gender value (eg 'M' or 'F'), an error will occur.
Do not worry! It is not case sensitive.
You can put any error text you want in the message.
You can also receive a null value through nullable. But remember that it defaults to 'false'.
์ด ๊ฒ์ฆ์ Enum์ ์์ฒญ์ ๊ฒ์ฆํ ์ ์์ต๋๋ค.
null ๊ฐ์ ํ์ธํ๊ณ ๊ฐ์ ํ์ธํ ์ ์์ต๋๋ค.
๋น์ ์ด ๋ง์ฝ gender๊ฐ์ 'MALE','FEMALE'์ ์ ์ธํ ๊ฐ (์๋ฅผ๋ค์ด, 'M' ํน์ 'F')์ ์ ๋ ฅํ๋ค๋ฉด, ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ ๋๋ค.
๊ฑฑ์ ํ์ง ๋ง์ธ์! ๋์๋ฌธ์๋ ๊ตฌ๋ถํ์ง ์์ต๋๋ค.
message์๋ ๋น์ ์ด ์ํ๋ ์๋ฌ ๋ฌธ๊ตฌ๋ฅผ ๋ฃ์ ์ ์์ต๋๋ค.
nullable์ ํตํด null๊ฐ์ ๋ฐ์ ์๋ ์์ต๋๋ค. ํ์ง๋ง ๊ทธ๊ฒ์ ๊ธฐ๋ณธ๊ฐ์ 'false'์์ ๊ธฐ์ตํ์ญ์์ค.
-
-
ListResponse
- This response will be mainly used by the Controller.
- When responding to a List, you will be able to send it as a common response.
- Please refer to the code below for how to use it.
์ด ์๋ต์ Controller์์ ์ฃผ๋ก ์ฌ์ฉ๋ ๊ฒ์ ๋๋ค.
List๋ฅผ ์๋ตํ ๋, ๊ณตํต์ ์ธ ์๋ต์ผ๋ก ๋ณด๋ผ ์ ์์ ๊ฒ์ ๋๋ค.
Content.java
public class Content { private String gender; private String age; }
Controller.java
@RestController public class Controller { @GetMapping public ListResponse<Content> test2(){ List<Content> content = new ArrayList<>(); //... return new ListResponse<>(content); } }
The response would be something like this:
{ "content" :[ { "gender" : "MALE", "age" : 18 } ], "totalCount" : 1 }