title: Multiple scenarios @RequestMapping produces JSON/XML together with Accept or ResponseEntity
body: <p>I am working with Spring 4.0.7 </p>
  
  <p>About Spring MVC, for research purposes, I have the following:</p>
  
  <pre><code>@RequestMapping(value="/getjsonperson", 
                  method=RequestMethod.GET, 
                  produces=MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody Person getJSONPerson(){
      logger.info("getJSONPerson - getjsonperson");
      return PersonFactory.createPerson();
  }
  
  @RequestMapping(value="/getperson.json", method=RequestMethod.GET)
  public @ResponseBody Person getPersonJSON(){
      logger.info("getPerson - getpersonJSON");
      return PersonFactory.createPerson();
  }
  </code></pre>
  
  <p>Each one works fine, observe both for JSON, with and without extension:</p>
  
  <ul>
  <li>/getjsonperson</li>
  <li>/getperson.json</li>
  </ul>
  
  <p>Same for XML</p>
  
  <pre><code>@RequestMapping(value="/getxmlperson",
                  method=RequestMethod.GET,
                  produces=MediaType.APPLICATION_XML_VALUE
                  )
  public @ResponseBody Person getXMLPerson(){
      logger.info("getXMLPerson - getxmlperson");
      return PersonFactory.createPerson();
  }
  
  @RequestMapping(value="/getperson.xml", method=RequestMethod.GET)
  @ResponseBody
  public Person getPersonXML(){
      logger.info("getPerson - getpersonXML");
      return PersonFactory.createPerson();
  }
  </code></pre>
  
  <p>Each one works fine, observe both for XML, with and without extension:</p>
  
  <ul>
  <li>/getxmlperson</li>
  <li>/getperson.xml</li>
  </ul>
  
  <p>Now about <strong>Restful</strong> I have the following:</p>
  
  <pre><code>@RequestMapping(value="/person/{id}/", 
                  method=RequestMethod.GET,
                  produces={MediaType.APPLICATION_JSON_VALUE, 
                            MediaType.APPLICATION_XML_VALUE})
  public ResponseEntity&lt;Person&gt; getPersonCustomizedRestrict(@PathVariable Integer id){
      Person person = personMapRepository.findPerson(id);
      return new ResponseEntity&lt;&gt;(person, HttpStatus.FOUND);//302     
  }
  </code></pre>
  
  <p>Observe the <code>MediaType</code>, it is mixed, for JSON and XML</p>
  
  <p>Through <strong>RestTemplate</strong> I can indicate the <code>Accept</code> value</p>
  
  <pre><code>    if(type.equals("JSON")){
          logger.info("JSON");
          headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
      }
      else if(type.equals("XML")){
          logger.info("XML");
          headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
      }
      ….
  
      ResponseEntity&lt;Person&gt; response =
                  restTemplate.exchange("http://localhost:8080/spring-utility/person/{id}/customizedrestrict",
                                        HttpMethod.GET,
                                        new HttpEntity&lt;Person&gt;(headers),  
                                        Person.class,
                                         id
                                       ); 
  </code></pre>
  
  <p>Until here, therefore I am able to use one URL/URI to get some data in either XML or JSON formats. It works fine</p>
  
  <p>My problem is with Spring MVC … just consider</p>
  
  <pre><code>@RequestMapping(value="/{id}/person", 
                  method=RequestMethod.GET,
                  produces={MediaType.APPLICATION_JSON_VALUE,  
                            MediaType.APPLICATION_XML_VALUE})
  public @ResponseBody Person getPerson(@PathVariable Integer id){
      return personMapRepository.findPerson(id);
  }
  </code></pre>
  
  <p>I can call or activate that handler method (<code>@RequestMapping</code>) through:</p>
  
  <ol>
  <li>jQuery working with Ajax, I am able to indicate the <code>Accept</code> value (JSON for example)</li>
  <li><a href="https://addons.mozilla.org/en-US/firefox/addon/poster/" rel="noreferrer">Poster</a>, through the <code>Headers</code> button, I can set the <code>Accept</code> </li>
  </ol>
  
  <p><strong>Question One:</strong></p>
  
  <p>But for a common link? how I can set the <code>Accept</code> value? is possible?</p>
  
  <p>I thought in other way to around this problem.</p>
  
  <ul>
  <li><code>http://localhost:8080/spring-utility/person/getpersonformat?format=json</code></li>
  <li><code>http://localhost:8080/spring-utility/person/getpersonformat?format=xml</code></li>
  </ul>
  
  <p>Observe: </p>
  
  <ul>
  <li><code>?format</code></li>
  </ul>
  
  <p>Therefore</p>
  
  <pre><code>@RequestMapping(value="/getpersonformat", 
                  method=RequestMethod.GET,
                  produces={MediaType.APPLICATION_JSON_VALUE,  
                            MediaType.APPLICATION_XML_VALUE})
  public @ResponseBody Person getPerson(@RequestParam String format){
      return personMapRepository.findPerson(id);
  }
  </code></pre>
  
  <p><strong>Question Two:</strong></p>
  
  <p>What code for the method shown above must be added to customize the return type format? 
  I mean, JSON or XML, Is possible?</p>
  
  <p>I thought in the following:</p>
  
  <pre><code>@RequestMapping(value="/getpersonformataltern",
          method=RequestMethod.GET
          produces={MediaType.APPLICATION_JSON_VALUE, 
                    MediaType.APPLICATION_XML_VALUE}
          )
  public ResponseEntity&lt;Person&gt; getPersonFormat(@RequestParam String format){
      logger.info("getPersonFormat - format: {}", format);
      HttpHeaders httpHeaders = new HttpHeaders();
      if(format.equals("json")){
          logger.info("Ok JSON");
          httpHeaders.setContentType(MediaType.APPLICATION_JSON);
      }
      else{
          logger.info("Ok XML");
          httpHeaders.setContentType(MediaType.APPLICATION_XML);
      }
      return new ResponseEntity&lt;&gt;(PersonFactory.createPerson(), httpHeaders, HttpStatus.OK);
  }
  </code></pre>
  
  <p>But: </p>
  
  <p>If I execute the URL:</p>
  
  <ul>
  <li><code>http://localhost:8080/spring-utility/person/getpersonformataltern?format=json</code></li>
  </ul>
  
  <p>I get </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;person&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;firstName&gt;Manuel&lt;/firstName&gt;
      &lt;lastName&gt;Jordan&lt;/lastName&gt;
  …
  &lt;/person&gt;
  </code></pre>
  
  <p>Yes in <strong>XML</strong>! </p>
  
  <p><strong>Note</strong>: I can confirm the Console prints <code>Ok JSON</code></p>
  
  <p>If I execute the URL:</p>
  
  <ul>
  <li><code>http://localhost:8080/spring-utility/person/getpersonformataltern?format=xml</code></li>
  </ul>
  
  <p>I get</p>
  
  <pre><code>This XML file does not appear to have any style information associated with it. 
  The document tree is shown below.
  
  &lt;person&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;firstName&gt;Manuel&lt;/firstName&gt;
      &lt;lastName&gt;Jordan&lt;/lastName&gt; 
      …
  &lt;/person&gt;
  </code></pre>
  
  <p><strong>Question Three</strong></p>
  
  <p>What code for the method shown above must be added to fix the JSON output? 
  I don't know what is wrong or is missing..</p>
  
  <p>There are three questions. </p>
  
  <p>Thank You</p>
  
  <p><strong>Alpha</strong></p>
  
  <pre><code>@Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
      Map&lt;String,MediaType&gt; mediaTypes = new LinkedHashMap&lt;&gt;();
      mediaTypes.put("json", MediaType.APPLICATION_JSON);
      mediaTypes.put("xml", MediaType.APPLICATION_XML);
      configurer.mediaTypes(mediaTypes);
      configurer.defaultContentType(MediaType.TEXT_HTML);
  }
  </code></pre>