spring

SpringMVC ArgumentResolver

devJK93 2024. 12. 2.

 

๐Ÿ“ Spring MVC์˜ ArgumentResolver๋ž€?

ArgumentResolver๋Š” Spring MVC์—์„œ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ•ด์„ํ•˜๊ณ  ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. Spring MVC์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ ํ๋ฆ„์—์„œ HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ํŠน์ •ํ•œ ํƒ€์ž…์˜ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ArgumentResolver๋ž€?

  • ์—ญํ• :
    • HTTP ์š”์ฒญ์˜ ๋ฐ์ดํ„ฐ (์˜ˆ: ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ, URL ๊ฒฝ๋กœ ๋ณ€์ˆ˜, ํ—ค๋”, ์š”์ฒญ ๋ณธ๋ฌธ ๋“ฑ)๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ณ€ํ™˜.
    • ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ๋งž์ถคํ˜• ๋กœ์ง ์ ์šฉ.
  • ์ฃผ์š” ์ธํ„ฐํŽ˜์ด์Šค:
    • HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค:
      • supportsParameter(MethodParameter parameter): ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜.
      • resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory): ์‹ค์ œ ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜.

Spring MVC์—์„œ์˜ ํ๋ฆ„

  1. HTTP ์š”์ฒญ ์ˆ˜์‹ : ์‚ฌ์šฉ์ž๊ฐ€ HTTP ์š”์ฒญ์„ ๋ณด๋ƒ„ (์˜ˆ: GET, POST ๋“ฑ).
  2. HandlerMapping: ์š”์ฒญ URI์™€ ์ผ์น˜ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์Œ.
  3. HandlerAdapter: ํ•ด๋‹น ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ค€๋น„. ์ด ๊ณผ์ •์—์„œ ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•ด ArgumentResolver๊ฐ€ ์‚ฌ์šฉ๋จ.
  4. ArgumentResolver ํ˜ธ์ถœ:
    • ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ์˜ ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฒ€์‚ฌ.
    • ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•ด ์ ์ ˆํ•œ HandlerMethodArgumentResolver๋ฅผ ์„ ํƒ.
    • ์„ ํƒ๋œ Resolver๊ฐ€ ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ƒ์„ฑ.
  5. ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ ์‹คํ–‰: ์ค€๋น„๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ํ•จ๊ป˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜.

๊ธฐ๋ณธ ์ œ๊ณต Resolver ์˜ˆ์‹œ

Spring์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ArgumentResolver๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

  • RequestParamMethodArgumentResolver: @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ.
  • PathVariableMethodArgumentResolver: @PathVariable ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ.
  • ModelAttributeMethodProcessor: @ModelAttribute๊ฐ€ ๋ถ™์€ ๊ฐ์ฒด๋ฅผ ์ฒ˜๋ฆฌ.
  • RequestBodyMethodArgumentResolver: @RequestBody ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ์š”์ฒญ ๋ณธ๋ฌธ ์ฒ˜๋ฆฌ.
  • HttpEntityMethodProcessor: HttpEntity ๋˜๋Š” RequestEntity ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ.

์‚ฌ์šฉ์ž ์ •์˜ ArgumentResolver

์‚ฌ์šฉ์ž ์ •์˜ ArgumentResolver๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค:

@Component
public class CustomArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        // ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ
        return parameter.getParameterType().equals(MyCustomType.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, 
                                  ModelAndViewContainer mavContainer, 
                                  NativeWebRequest webRequest, 
                                  WebDataBinderFactory binderFactory) throws Exception {
        // ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ MyCustomType ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
        return new MyCustomType(webRequest.getParameter("customParam"));
    }
}

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CustomArgumentResolver());
    }
}

Spring MVC ํ๋ฆ„์—์„œ ArgumentResolver์˜ ์œ„์น˜

ArgumentResolver๋Š” Spring MVC์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „์— ์‚ฌ์šฉ๋˜๋ฉฐ, HTTP ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ ์ ˆํžˆ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฉ”์ปค๋‹ˆ์ฆ˜ ๋•๋ถ„์— ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€