title: How to reflect the incoming GET request in text?
body: <p>I'm using Spring MVC and Springboot (assume latest version) to put up a simple web service that returns some text. </p>
  
  <p>I was able to figure out how to use <strong>@RequestMapping</strong> and <strong>@PathVariable</strong> annotations to show one of the URL path variables as a response from the server (e.g. if the user goes to .../my_user_id/ in the browser, they can then see some text in the browser that includes that user_id... since the service returned it as a response).</p>
  
  <p><em>I need help with figuring out how to</em> capture the GET HTTP request the user's browser makes and then display it in text form as a response in the browser (I want to display the headers and the request body as plain text). </p>
  
  <p>I've done some research, but none of the solutions available work properly. Is anyone aware of the right approach / feasibility here? </p>
  
  <p>An approach I tried:</p>
  
  <ul>
  <li><a href="http://www.mkyong.com/java/how-to-get-http-request-header-in-java/" rel="nofollow noreferrer">http://www.mkyong.com/java/how-to-get-http-request-header-in-java/</a></li>
  </ul>
  
  <p>Some threads on the error I get back when I tried the above approach:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7952154/spring-resttemplate-how-to-enable-full-debugging-logging-of-requests-responses">Spring RestTemplate - how to enable full debugging/logging of requests/responses?</a></li>
  <li><a href="http://forum.spring.io/forum/spring-projects/roo/84244-circular-view-path-resourcenotfound" rel="nofollow noreferrer">http://forum.spring.io/forum/spring-projects/roo/84244-circular-view-path-resourcenotfound</a></li>
  <li><a href="https://stackoverflow.com/questions/10567341/circular-view-path">Circular view path</a></li>
  <li><a href="https://stackoverflow.com/questions/18813615/how-to-avoid-the-circular-view-path-exception-with-spring-mvc-test">How to avoid the &quot;Circular view path&quot; exception with Spring MVC test</a></li>
  <li><a href="http://myshittycode.com/2014/01/17/mockmvc-circular-view-path-view-would-dispatch-back-to-the-current-handler-url-view-again/" rel="nofollow noreferrer">http://myshittycode.com/2014/01/17/mockmvc-circular-view-path-view-would-dispatch-back-to-the-current-handler-url-view-again/</a></li>
  </ul>
  
  <p>More on Spring MVC, which I'm heavily using:</p>
  
  <ul>
  <li><a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html" rel="nofollow noreferrer">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html</a></li>
  </ul>
  
  <p>Anyhow. When using the below @Controller, I get an error:</p>
  
  <pre><code>@RestController
  public class HelloController {
  
  @RequestMapping("/")
  public String index() {
      return "Welcome to your home directory";
  }
  
  @RequestMapping(value="/mydata/{userId}", method=RequestMethod.GET)
  public String printTheUser(@PathVariable String userId) {
      return "The data for " + userId + " would live here";
  }
  
  
  @RequestMapping("/summary_data/")
  public String index3() {
      return "All summary data would appear here";
  }
  
  private String server = "localhost";
  private int port = 8080;
  
  @RequestMapping("/request_mirror/**")
  public @ResponseBody String mirrorRest(@RequestBody String body, HttpMethod method, HttpServletRequest request,
      HttpServletResponse response) throws URISyntaxException {
      URI uri = new URI("http", null, server, port, request.getRequestURI(), request.getQueryString(), null);
  
      RestTemplate restTemplate = new RestTemplate();
  
      ResponseEntity&lt;String&gt; responseEntity =
          restTemplate.exchange(uri, method, new HttpEntity&lt;String&gt;(body), String.class);
  
      return responseEntity.getBody();
      }
  }
  </code></pre>
  
  <p>When running this code and navigating to localhost:8080/request_mirror/stuff3/, I get the following error:</p>
  
  <pre><code>Whitelabel Error Page.   This application has no explicit mapping for /error, so you are seeing this as a fallback.
  Mon Feb 08 15:41:13 EST 2016
  There was an unexpected error (type=Bad Request, status=400).
  Required request body content is missing:    org.springframework.web.method.HandlerMethod$HandlerMethodParameter@a35a9b3f
  </code></pre>
  
  <p>Now, when I try a different approach (another @Controller) - the code looks like:</p>
  
  <pre><code>@Controller
  @RequestMapping("/site")
  public class SecondController{
  
  @Autowired
  private HttpServletRequest request;
  
  @RequestMapping(value = "/{input:.+}", method = RequestMethod.GET)
  public ModelAndView getDomain(@PathVariable("input") String input) {
  
      ModelAndView modelandView = new ModelAndView("result");
  
      modelandView.addObject("user-agent", getUserAgent());
      modelandView.addObject("headers", getHeadersInfo());
  
      return modelandView;
  
  }
  
  //get user agent
  private String getUserAgent() {
      return request.getHeader("user-agent");
  }
  
  //get request headers
  private Map&lt;String, String&gt; getHeadersInfo() {
  
      Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
      Enumeration headerNames = request.getHeaderNames();
      while (headerNames.hasMoreElements()) {
          String key = (String) headerNames.nextElement();
          String value = request.getHeader(key);
          map.put(key, value);
      }
  
      return map;
  }
  }
  </code></pre>
  
  <p>For the above code (SecondController), (sourced from <a href="http://www.mkyong.com/java/how-to-get-http-request-header-in-java/" rel="nofollow noreferrer">http://www.mkyong.com/java/how-to-get-http-request-header-in-java/</a>),  I get the following error, when I try to navigate to localhost:8080/site/stuff123456789... (but I can see the header keys and values from the request in the Map upon inspection... just not sure how to display them as text in the browser as the response).</p>
  
  <pre><code>This application has no explicit mapping for /error, so you are seeing this as a fallback.
  
   Mon Feb 08 16:10:47 EST 2016
  There was an unexpected error (type=Internal Server Error, status=500).
  Circular view path [stuff123456789]: would dispatch back to the current handler URL [/site/stuff123456789] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
  </code></pre>