• ์๋ ํ์ธ์~ ์ด์ ์ ์ด์ํ๋ ๋ธ๋ก๊ทธ์ ์์ค์ฝ๋๋ฅผ ๊ด๋ฆฌํ๋ 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
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring Boot Maven profile ์ด์ & ๊ฐ๋ฐ DB ๋ถ๋ฆฌ(AWS EC2) (6) | 2021.06.18 |
---|---|
JUnit - @ParameterizedTest, @ValueSource, @CsvSource, @MethodSource ์ด๋ ธํ ์ด์ (4) | 2021.06.08 |
[Spring] ๊ฐ์ฒด๋ณต์ฌ BeanUtils.copyProperties() & ์๋ ์๋ฆฌ (0) | 2021.02.20 |
[Spring] Maven ๋ฉํฐ๋ชจ๋ ํ๋ก์ ํธ (0) | 2021.02.09 |
[Spring] - @ModelAttribute, @RequestParam (0) | 2021.02.02 |
๋๊ธ