[Back-end] Spring - Interceptor(Servlet)에서 @PathVariable값 가져오기

유네스코d·2023년 9월 26일

Back-End

목록 보기
3/3

request url이 다음과 같다면

@RequestMapping("/{id}")

Spring Controller에서는 @PathVariable로 값을 가져올 수 있다. 하지만 Interceptor는 Servlet 기반으로 코드를 작성해야 하기 때문에 해당 어노테이션을 사용하지 못한다.

@PathVariable("id") String blogId

Servlet 기반 코드에서 @PathVariable 데이터를 가져오고 싶다면??? 이렇게 가져오면 된다!!

public class AdminInterceptor extends HandlerInterceptorAdapter{
    @Override
  	public boolean preHandle(final HttpServletRequest request,final HttpServletResponse response,final Object handler)
  		throws Exception {
        	
            Map<String, String> pathVariables = (Map<String, String>) request
            									.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

  		}
}

Map에 PathVariable의 name과 value를 자동으로 매핑해줌!

profile
yune's coding

1개의 댓글

comment-user-thumbnail
2024년 9월 19일

오오오 감사합니다!!!

답글 달기