title: Controller for JSON and HTML with Spring Boot
body: <p>I am writing an application where among other things I need to do CRUD operations with certain objects. I need to be able to serve both HTML pages for human users, and JSON for other applications. Right now my URLs look like this for "Read":</p>
  
  <pre><code>GET  /foo/{id}      -&gt; Serves HTML
  GET  /rest/foo/{id} -&gt; Serves JSON
  etc.
  </code></pre>
  
  <p>This seems a little redundant. I would rather have something like this:</p>
  
  <pre><code>GET /foo/{id}.html OR /foo/{id} -&gt; Serves HTML
  GET /foo/{id}.json              -&gt; Serves JSON
  </code></pre>
  
  <p>Can Spring Boot do this? If so, how?</p>
  
  <p>I know how to return JSON:</p>
  
  <pre><code>@RequestMapping(value = "/foo/{id}", method = RequestMethod.GET, produces = "application/json")
  public Object fetch(@PathVariable Long id) {
      return ...;
  }
  </code></pre>
  
  <p>I also know how to return HTML:</p>
  
  <pre><code>@RequestMapping("/app/{page}.html")
  String index(@PathVariable String page) {
      if(page == null || page.equals(""))
          page = "index";
      return page;
  }
  </code></pre>
  
  <p>But I'm not sure how to have a controller do one or the other based on the request.</p>