๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] HandlerMethodArgumentResolver

by ์ฃผ๋ฐœ2 2021. 1. 30.
๋ฐ˜์‘ํ˜•

 ์•ˆ๋…•ํ•˜์„ธ์š”~ ์ด์ „์— ์šด์˜ํ•˜๋˜ ๋ธ”๋กœ๊ทธ์™€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” GitHub๊ฐ€ ๋”ฐ๋กœ ์žˆ์Šต๋‹ˆ๋‹ค.

 ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ

 GitHub

๐Ÿ”


โœ” HandlerMethodArgumentResolver ์„ค๋ช…

 

 

์•ˆ๋…•ํ•˜์„ธ์š”~ ์ด๋ฒˆ์— ์ •๋ฆฌํ•  ๋‚ด์šฉ์€ Spring์˜ HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค ์ž…๋‹ˆ๋‹ค.

HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค๋Š” Controller์—์„œ ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ์„๋•Œ ํ•ด๋‹น ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.

 

๋ณดํ†ต ์Šคํ”„๋ง์˜ Controller์—์„œ @RequestBody ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด request์˜ Body ๊ฐ’์„ ๋ฐ›์•„์˜ฌ๋•Œ

HandlerMethodArgumentResolver ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉ ์‹œ์ผœ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค.

 

HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์œ„์™€ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋Š” ์•„๋ž˜ ๋‘ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

boolean supportsParameter(MethodParameter var1)

 

Object resolveArgument(

        MethodParameter var1,

        @Nullable ModelAndViewContainer var2,

        NativeWebRequest var3, 

        @Nullable WebDataBinderFactory var4)

 

 

๊ฐ ๋ฉ”์„œ๋“œ์™€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์˜๋ฏธํ•˜๋Š”๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 supportsParameter 

    • ํ˜„์žฌ resolver๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์›ํ•˜๋ฉด true๋ฅผ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค

 

 resolveArgument

    • ์‹ค์ œ ๋ฐ”์ธ๋”ฉ ํ•  ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

    • ๋ฐ”์ธ๋”ฉ ํ•  ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ

    • ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     MethodParameter var1

         ํ™•์ธํ•  ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜

         ์ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์œ„ supportsParameter์— ์˜ํ•ด true๊ฐ€ ๋ฐ˜ํ™˜๋˜์–ด์•ผ ํ•จ

     ModelAndViewContainer var2

         ํ˜„์žฌ request์— ๋Œ€ํ•œ ModelAndViewContainer

     NativeWebRequest var3

         ํ˜„์žฌ request

     WebDataBinderFactory var4

         WebDataBinder ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์„ ์œ„ํ•œ Factory


 

โœ” HandlerMethodArgumentResolver ์˜ˆ์ œ

 

 

์ด์ œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

1) Person ๊ฐ์ฒด ์ƒ์„ฑ

package com.github.prgrms.social.model.person;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public class Person {

    private final String name;
    private final int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Person(String name, int age) {
        checkNotNull(name, "name must be provided.");
        checkArgument(age >= 0, "age must be greater than zero");

        this.name = name;
        this.age = age;
    }
}

 

 

 

2) resolver ์ž‘์„ฑ

package com.github.prgrms.social.model.person;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class CustomHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    private static final String DEFAULT_PERSON_NAME = "JuHyun";
    private static final int DEFAULT_PERSON_AGE = 29;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return Person.class.isAssignableFrom(methodParameter.getParameterType());
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return new Person(DEFAULT_PERSON_NAME, DEFAULT_PERSON_AGE);
    }
}

 

 

 

3) resolver ๋“ฑ๋ก

package com.github.prgrms.social.configure;

import com.github.prgrms.social.model.person.CustomHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {

    private final CustomHandlerMethodArgumentResolver customHandlerMethodArgumentResolver;

    public WebMvcConfigure(CustomHandlerMethodArgumentResolver customHandlerMethodArgumentResolver) {
        this.customHandlerMethodArgumentResolver = customHandlerMethodArgumentResolver;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(customHandlerMethodArgumentResolver);
    }
}

 

 

 

4) Controller ์ž‘์„ฑ

package com.github.prgrms.social.controller;

import com.github.prgrms.social.model.person.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PersonController {

    @GetMapping("/person")
    public Person test(Person person) {
        return person;
    }
}

 

HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋‹จ๊ณ„๋ฅผ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

1) ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์„ ํด๋ž˜์Šค ์ƒ์„ฑ

2) HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด ์ƒ์„ฑ

3) HandlerMethodArgumentResolver ๊ตฌํ˜„์ฒด resolver ๋“ฑ๋ก

4) Controller ์ž‘์„ฑ

 

 

์œ„ ๊ณผ์ •์„ ํ†ตํ•ด ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ ํ•œ ํ›„ localhost:8080/person ์˜ URL๋กœ ์ ‘์†ํ•˜๋ฉด

resolver์— ๋“ฑ๋กํ•œ ์ดˆ๊ธฐ Person ๊ฐ์ฒด์˜ ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 


 

โœ” References

velog.io/@kingcjy/Spring-HandlerMethodArgumentResolver%EC%9D%98-%EC%82%AC%EC%9A%A9%EB%B2%95%EA%B3%BC-%EB%8F%99%EC%9E%91%EC%9B%90%EB%A6%AC

 

docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/method/support/HandlerMethodArgumentResolver.html

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€