title: How to parse a URL and run a method with Spring MVC 'reflectively'?
body: <p>I have a Spring Boot application that uses Spring MVC in the usual manner, with a bunch of <code>@RequestMapping</code> methods, Freemarker definitions, and the like. This is all tied together with a <code>WebMvcConfigurerAdapter</code> class.</p>
  
  <p>I'd like to provide a service where the user submits a list of valid URLs, and the webapp would work out which controller would be called, passes in the parameters, and returns a combined result for every URL — all in one request.</p>
  
  <p>This would save the user from having to make hundreds of HTTP calls, but would still allow them to make one-off requests if need be. Ideally, I'd just inject an auto-configured Spring bean, so I don't have to repeat the URL resolving and adapting and handling that Spring does internally, and the controller's list of other controllers would never go out of sync with the <em>real</em> list of controllers.</p>
  
  <p>I expected to write something like this (simplified to only deal with one URL, which is pointless but easier to understand):</p>
  
  <pre><code>@Autowired BeanThatSolvesAllMyProblems allMappings;
  
  @PostMapping(path = "/encode", consumes = MediaType.TEXT_PLAIN_VALUE)
  @ResponseBody
  public String encode(@RequestBody String inputPath) {
      if (allMappings.hasMappingForPath(inputPath)) {
          return allMappings.getMapping(inputPath).execute();
      } else {
          return "URL didn't match, sorry";
      }
  }
  </code></pre>
  
  <p>Instead, I've had to define Spring beans I don't know what they do and have been repeating some of what Spring is meant to do for me, which I'm worried won't work <em>quite</em> the same as it would if the user just made the call themselves:</p>
  
  <pre><code>// these two are @Beans, with just their default constructor called.
  @Autowired RequestMappingHandlerMapping handlers;
  @Autowired RequestMappingHandlerAdapter adapter;
  
  @PostMapping(path = "/encode", consumes = MediaType.TEXT_PLAIN_VALUE)
  @ResponseBody
  public String encode(@RequestBody String inputText) {
      final HttpServletRequest mockRequest = new MockHttpServletRequest(null, inputText);
      final StringBuilder result = new StringBuilder();
  
      this.handlers.getHandlerMethods().forEach((requestMappingInfo, handlerMethod) -&gt; {
          if (requestMappingInfo.getPatternsCondition().getMatchingCondition(mockRequest) != null) {
              try {
                  final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
                  result.append("Result: ").append(adapter.handle(mockRequest, mockResponse, handlerMethod));
                  result.append(", ").append(mockResponse.getContentAsString());
                  result.append("\n");
              } catch (Exception e) {
                  logger.error(e.getMessage(), e);
              }
          }
      });
  
      return result.toString();
  }
  </code></pre>
  
  <p>I thought I was doing quite well going down this path, but it's failing with <code>Missing URI template variable</code> errors, and not only do I have no idea how to put the request parameters in (another thing which Spring could be able to handle itself), but I'm not even sure that this is the right way to go about doing this. So how do I simulate a Spring MVC request "reflectively", from within the webapp itself?</p>