Skip to content

Latest commit

ย 

History

History
116 lines (85 loc) ยท 3.18 KB

Validator.md

File metadata and controls

116 lines (85 loc) ยท 3.18 KB

Validator

์ •์˜

public interface Validator {
	boolean supports(Class<?> clazz); // ๊ฒ€์ฆ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ํ™•์ธ
	void validate(Object target,Errors errors); // ๊ฒ€์ฆ
}

์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•ด ๊ฒ€์ฆ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๊ฒ€์ฆํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค

๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ์ „์—ญ ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰
  2. ์ผ๋ถ€(์ปจํŠธ๋กค๋Ÿฌ) ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰

๊ตฌํ˜„

Validator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•œ๋‹ค.

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class RegisterRequestValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        System.out.println("clazz: " + clazz.toString());
        return true;
    }

    @Override
    public void validate(Object target, Errors errors) {
        System.out.println("target: " + target.toString());
        System.out.println("errors: " + errors.toString());
    }

}

์ „์—ญ ๋ฒ”์œ„์˜ Validator

Validator๋ฅผ ํ†ตํ•ด ์ „์—ญ ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฆ„์ด ์ •ํ•ด์ง„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

1. @Configuration ๋‚ด Bean์œผ๋กœ ์ง์ ‘ ๋“ฑ๋ก (getValidator ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์•ผ ํ•จ)

@Override
public Validator getValidator() {
    return new RegisterRequestValidator();
}

2. ๊ฒ€์ฆ์ด ํ•„์š”ํ•œ ์ปจํŠธ๋กค๋Ÿฌ ๋‚ด ๋ฉ”์†Œ๋“œ์— @Valid ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
public class HelloController {

    @GetMapping("/valid")
    @ResponseBody
    public String valid(@Valid ValidDto dto, Errors errors) {
        if (errors.hasErrors()) System.out.println("has error!");
        return "validation OK";
    }

}

์ปจํŠธ๋กค๋Ÿฌ ๋ฒ”์œ„์˜ Validator

Validator๋ฅผ ํ†ตํ•ด ์ปจํŠธ๋กค๋Ÿฌ ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ @InitBinder ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
public class HelloController {

    @GetMapping("/valid")
    @ResponseBody
    public String valid(@Valid ValidDto dto, Errors errors) {
        if (errors.hasErrors()) System.out.println("has error!");
        return "validation OK";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new RegisterRequestValidator());
    }

}

ํ•ด๋‹น ์ปจํŠธ๋กค๋Ÿฌ ๋‚ด ๋ชจ๋“  ๋ฉ”์„œ๋“œ์— Validation์ด ์ ์šฉ๋œ๋‹ค.