title: Spring REST - How to enforce response type being JSON when URL ends with .xml
body: <p>My Spring REST API has trouble (=> <code>406</code>) when an endpoint ends with a file name, e.g. <code>/file/example.xml</code> because it assumes the result should be XML but thats wrong (it should be JSON).</p>
  
  <p><code>Written [{timestamp=Thu Oct 20 11:11:14 CEST 2016, status=406, error=Not Acceptable, exception=org.springframework.web.HttpMediaTypeNotAcceptableException, message=Could not find acceptable representation, path=/file/foobar.xml}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7015ebef]</code></p>
  
  <p>I read something about a Spring 4 feature detecting the file extensions and overwritting the response type, so I tried to enforce the response type but I did not find a solution.</p>
  
  <p>Here is my REST controller:</p>
  
  <pre><code>@CompileStatic
  @RestController
  class ImportFileController {
      @RequestMapping(
              value = "/file/{fileName:.+}",
              method = RequestMethod.PUT,
              produces = "application/json"
      )
      @ResponseBody
      public ImportFile update(
              @PathVariable("fileName") String fileName,
              @RequestBody ImportFile importFile) {
          // ..
          return importFile
      }
  }
  </code></pre>
  
  <p>This is the request/response:</p>
  
  <pre><code>Request PUT /file/foobar.xml {"reader_name":"something"}
  Response 406 {
    "timestamp" : "2016-10-20 08:56:35",
    "status" : 406,
    "error" : "Not Acceptable",
    "exception" : "org.springframework.web.HttpMediaTypeNotAcceptableException",
    "message" : "Could not find acceptable representation",
    "path" : "/file/foobar.xml"
  }
  </code></pre>
  
  <p>What I tried:</p>
  
  <ul>
  <li>Using <code>{filenName:.+}</code> instead of <code>{fileName}</code></li>
  <li>Using <code>produces</code> in my <code>@RequestMapping</code></li>
  <li>Sending a custom <code>Accept</code> header (<code>application/json</code>)</li>
  <li>Setting <code>spring.mvc.path-matching.suffix-pattern</code> to <code>true</code> or <code>false</code> but that was just a wild guess</li>
  </ul>
  
  <p>What worked:</p>
  
  <ul>
  <li>Changing endpoint from <code>/file/{fileName:.+}</code> to <code>/file/{fileName:.+}/update</code>
  But that's just a workaround, not a solution.</li>
  </ul>
  
  <p><strong>Important</strong>: I'm using Groovy and I'm using YML configuration.</p>