반응형
Argument Resolver ?
- 컨트롤러 메소드의 인자로 사용자가 임의의 값을 전달하는 방법을 제공하고자할 때 사용하는 방법
User
@Builder
@Getter
public class User {
private String id;
}
UserArgumentResolver
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterType() == User.class;
}
@Override
public Object resolveArgument(
MethodParameter parameter,
ModelAndViewContainer container,
NativeWebRequest request,
WebDataBinderFactory webDataBinderFactory
) throws Exception {
return User.builder()
.id(request.getHeader("id"))
.build();
}
}
WebMvcConfig
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new UserArgumentResolver());
}
}
UserController
@RestController
public class UserController {
@GetMapping("/api/user")
public User getUser(User user) {
return user;
}
}
요청
curl --location --request GET 'http://localhost:8080/api/user' \
--header 'id: helloworld'
응답
{
"id": "helloworld"
}
반응형
'Development > Spring' 카테고리의 다른 글
[Spring] Cache (with Redis) (0) | 2020.12.27 |
---|---|
[Spring] Swagger (0) | 2020.12.27 |
[Spring] Filter & Interceptor (0) | 2020.12.27 |
[Spring] Validation (0) | 2020.12.27 |
[Spring] BeanFactory & ApplicationContext (0) | 2020.12.27 |