Home > Blockchain >  Spring: What happens when request matches multiple RequestMapping values?
Spring: What happens when request matches multiple RequestMapping values?

Time:07-19

If I have two controllers:

@RestController
public class ContentController {

    @GetMapping("/specialContent")
    public Map<String, String> handleSpecialContent() {
        Map<String, String> map = handleContent("specialContent");
        map.put("special", "true");
        return map;
    }

    @GetMapping("/{content}")
    public Map<String, String> handleContent(@PathVariable String content) {
        HashMap<String, String> map = new HashMap<>();
        map.put("content", content);
        return map;
    }

} 

Is it guaranteed to go to handleSpecialContent() if I type '/specialContent' in the URL? I could not find this on the internet

Thanks

CodePudding user response:

Spring will match the handler to your explicitly defined mapping, so if you send a GET request to /specialContent, it's going to invoke your method marked as @GetMapping("/specialContent").

If you'd like to learn how Spring matches, set a breakpoint in org.springframework.web.servlet.DispatcherServlet#getHandler(HttpServletRequest request) and send your request.

  • Related