Skip to content

doohee94/common

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Common

INFO

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'

Functions

Currently, the following features are included. Many features will be added in the future. ๐Ÿ˜€

ํ˜„์žฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์ด ํฌํ•จ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์ด ์ถ”๊ฐ€๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

Validations

  • 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'์ž„์„ ๊ธฐ์–ตํ•˜์‹ญ์‹œ์˜ค.

Responses

  • 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
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages