๐Ÿ“Œ Spring MVC - HandlerMapping ์™„์ „ ์ •๋ฆฌ

My Pale Blue Dotยท2025๋…„ 4์›” 29์ผ
0

SPRING

๋ชฉ๋ก ๋ณด๊ธฐ
27/36
post-thumbnail

๐Ÿ“… 2025-04-29


๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ


1๏ธโƒฃ HandlerMapping ๊ฐœ์š”

โœ… HandlerMapping์ด๋ž€?

  • Spring MVC์—์„œ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ๋ฐ›์•„ ์–ด๋–ค ํ•ธ๋“ค๋Ÿฌ(์ปจํŠธ๋กค๋Ÿฌ)๋กœ ์ „๋‹ฌํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์ „๋žต ์ธํ„ฐํŽ˜์ด์Šค
  • DispatcherServlet โ†’ HandlerMapping โ†’ Controller ํ˜ธ์ถœ ๊ตฌ์กฐ
  • ๋Œ€ํ‘œ์ ์ธ ๊ตฌํ˜„์ฒด:
    • BeanNameUrlHandlerMapping
    • SimpleUrlHandlerMapping
    • RequestMappingHandlerMapping

2๏ธโƒฃ HandlerMapping ์„ค์ • ์ฝ”๋“œ

โœ… BeanNameUrlHandlerMapping

  • ์š”์ฒญ URL๊ณผ Bean ์ด๋ฆ„์„ ๋งค์นญ
  • ์˜ˆ์‹œ ์ฝ”๋“œ:
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
    return new BeanNameUrlHandlerMapping();
}

@Bean("/custom_01")
public CustomHandler customHandler() {
    return new CustomHandler();
}

โœ… SimpleUrlHandlerMapping

  • ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ URL-Handler๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ์‹
  • ์ฃผ๋กœ ์ •์  ์ž์›(css, js ๋“ฑ)์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์ •๋œ๋‹ค.
  • ์˜ˆ์‹œ ์ฝ”๋“œ:
@Bean
SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();

    Map<String, Object> urlMap = new HashMap<>();
    urlMap.put("/custom_02", new CustomHandler());
    handlerMapping.setUrlMap(urlMap);

    return handlerMapping;
}

โœ… RequestMappingHandlerMapping

  • @RequestMapping, @GetMapping, @PostMapping ๋“ฑ์„ ํ†ตํ•œ ๋งคํ•‘
  • ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ๋Š” ๊ธฐ๋ณธ ์„ค์ •
  • ์ง์ ‘ ๋“ฑ๋กํ•˜๋Š” ์˜ˆ์‹œ:
@Bean
RequestMappingHandlerMapping requestMappingHandlerMapping() throws NoSuchMethodException, SecurityException {
    RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();

    RequestMappingInfo mappingInfo = RequestMappingInfo
            .paths("/custom_03")
            .methods(RequestMethod.GET)
            .build();

    Method method = CustomHandler.class.getMethod("hello", null);

    handlerMapping.registerMapping(mappingInfo, new CustomHandler(), method);

    return handlerMapping;
}

3๏ธโƒฃ HandlerMapping์„ ์ปค์Šคํ…€ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

โœ… ๊ธฐ๋ณธ ์ œ๊ณต ๋งคํ•‘๋งŒ์œผ๋กœ๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์„ ๋•Œ

  • ์™ธ๋ถ€ ์‹œ์Šคํ…œ ์—ฐ๋™์ด๋‚˜, ๊ทœ์น™ ์—†๋Š” ๋‹ค์–‘ํ•œ URL ๋Œ€์‘์ด ํ•„์š”ํ•  ๋•Œ
  • ๋ณต์žกํ•œ ์š”์ฒญ ๋ถ„๊ธฐ ๋กœ์ง์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ

โœ… ์š”์ฒญ URL โ†’ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์œผ๋กœ ์ œ์–ดํ•˜๊ณ  ์‹ถ์„ ๋•Œ

  • ๋Ÿฐํƒ€์ž„์— ๋งคํ•‘์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•ด์•ผ ํ•  ๊ฒฝ์šฐ
  • ์„ค์ • ํŒŒ์ผ์„ ์ฝ์–ด ๋งคํ•‘์„ ๋™์ ์œผ๋กœ ์ ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

โœ… ๋ณต์žกํ•œ ๋งคํ•‘ ์ •์ฑ…์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

  • ๋‹จ์ˆœ ๊ฒฝ๋กœ ๋งค์นญ์ด ์•„๋‹ˆ๋ผ, ์š”์ฒญ Header, Content-Type, IP ์ฃผ์†Œ ๋“ฑ์— ๋”ฐ๋ผ ๋งคํ•‘์„ ๋‹ฌ๋ฆฌํ•ด์•ผ ํ•  ๋•Œ

โœ… ์Šคํ”„๋ง ๊ธฐ๋ณธ ๋งคํ•‘ ๋ฐฉ์‹์„ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ๋ณด์™„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

  • ๊ธฐ์กด ์ปจํŠธ๋กค๋Ÿฌ ๋งคํ•‘์€ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ณ„๋„ ์ปค์Šคํ…€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ•  ๋•Œ

4๏ธโƒฃ CustomHandler ํด๋ž˜์Šค

โœ… CustomHandler ์˜ˆ์‹œ

// Custom URL Handler ํด๋ž˜์Šค
public class CustomHandler implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("CustomHandler's handleRequest");
        return null;
    }

    public void hello() {
        System.out.println("CustomHandler's hello!!...");
    }
}

5๏ธโƒฃ ์„œ๋ฒ„ ์‹คํ–‰ ๊ฒฐ๊ณผ

๐Ÿ”น ๋งคํ•‘ ํ™•์ธ ๋กœ๊ทธ

  • BeanNameUrlHandlerMapping:
    Mapped URL path [/custom_01] onto handler '/custom_01'
    
  • SimpleUrlHandlerMapping:
    Mapped URL path [/custom_02] onto handler of type [class com.example.app.handler.CustomHandler]
    
  • RequestMappingHandlerMapping:
    Mapped "{[/custom_03],methods=[GET]}" onto public void com.example.app.handler.CustomHandler.hello()
    

๐Ÿ”น ์ถœ๋ ฅ ๊ฒฐ๊ณผ

  • /custom_01 ์š”์ฒญ ์‹œ:
    CustomHandler's handleRequest
    
  • /custom_03 ์š”์ฒญ ์‹œ (GET):
    CustomHandler's hello!!...
    

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


๐Ÿง  ๋А๋‚€ ์ 

  • Spring MVC์˜ ํ•ต์‹ฌ ์ฒ˜๋ฆฌ ๊ณผ์ •์ธ HandlerMapping์˜ ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ๋ฐฉ์‹์„ ์ฝ”๋“œ๋กœ ์ง์ ‘ ๊ตฌ์„ฑํ•ด๋ณด๋ฉด์„œ ๋™์ž‘ ์›๋ฆฌ๋ฅผ ๋ช…ํ™•ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ํŠนํžˆ RequestMappingHandlerMapping์„ ํ†ตํ•ด Reflection์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ๊ต‰์žฅํžˆ ํฅ๋ฏธ๋กœ์› ๋‹ค.
  • ์‹ค์Šต์„ ํ†ตํ•ด DispatcherServlet์ด ๋‹ค์–‘ํ•œ HandlerMapping์„ ํ†ตํ•ด ์š”์ฒญ์„ ๋งคํ•‘ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ์ „์ฒด ๊ณผ์ •์„ ์ฒดํ—˜ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

โœ๏ธ ์š”์•ฝ

  • HandlerMapping์€ ์š”์ฒญ URL๊ณผ Controller/Method๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํ•ต์‹ฌ ์ธํ„ฐํŽ˜์ด์Šค๋‹ค.
  • ์ฃผ์š” ๊ตฌํ˜„์ฒด:
    • BeanNameUrlHandlerMapping: Bean ์ด๋ฆ„๊ณผ URL ๋งค์นญ
    • SimpleUrlHandlerMapping: ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๋งคํ•‘ ์„ค์ •
    • RequestMappingHandlerMapping: @RequestMapping ๊ธฐ๋ฐ˜ ๋งคํ•‘
  • ์ปค์Šคํ…€ HandlerMapping์€ ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋™์ ์ธ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ํ•„์ˆ˜์ ์ด๋‹ค.
  • ์„œ๋ฒ„ ๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ๋งคํ•‘ ๊ณผ์ •์„ ๋ช…ํ™•ํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€