title: Spring MVC for Rest: How set a Locale value
body: <p>I am working with Spring Framework <code>4.3.2</code></p>
  
  <p>About <code>Rest</code> for <code>POST</code> and <code>PUT</code> the following situation happens.</p>
  
  <p>For <code>POST</code> I have the following:</p>
  
  <pre><code>@PostMapping(consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
  public ResponseEntity&lt;Void&gt; saveOne(@Validated @RequestBody Persona persona){
  </code></pre>
  
  <p>Observe the method has declared <code>@Validated</code>. </p>
  
  <p>I need do the validation and return the error text message according the <code>Locale</code> sent it by the client/user. </p>
  
  <p>I can do it through <code>Spring MVC Test</code>, for that I have the following:</p>
  
  <pre><code>resultActions = mockMvc.perform(post(uri).contentType(MediaType.APPLICATION_XML)
                       .accept(MediaType.APPLICATION_XML)
                       .locale(locale)
                       .content(...content...))).andDo(print());
  </code></pre>
  
  <p>Observe the <code>.locale(locale)</code> part.</p>
  
  <p>Until here about <code>JUnit</code> through <code>@Parameters</code> I am able to send a list of <code>Locale</code>s and thus see the error message in different languages. Until here all go how is expected</p>
  
  <p>Now, the other way to access a Rest controller is through <code>RestTemplate</code></p>
  
  <p>I have the following:</p>
  
  <pre><code>RequestEntity&lt;Persona&gt; requestEntity = RequestEntity.post(uri)
                 .contentType(MediaType.APPLICATION_JSON_UTF8)
                 .accept(MediaType.APPLICATION_JSON_UTF8)                                                               
                 .body(personaValid);
  
  ....
  
  ResponseEntity&lt;GenericCollection&lt;ValidatedFieldErrorReport&gt;&gt; responseEntity_ 
    = restTemplate.exchange(requestEntity, parameterizedTypeReference);
  </code></pre>
  
  <p>Sadly <code>RequestEntity</code> through the <code>post</code> does not support <code>.locale(locale)</code></p>
  
  <p>Even If I add <code>.header("Accept-Language", locale.toString())</code> it does not work, same consideration for <code>.header("locale", locale.toString())</code>. </p>
  
  <p><strong>Note</strong>: I can confirm when I print the <code>RequestEntity</code> object it sends the expected <code>Locale</code> <strong>but</strong> in the server: it ignores the 'locale' sent it (<em>how if it never has been not sent it from the beginning</em>) and uses the default <code>Locale</code> available in the server. I am confused with this approach.</p>
  
  <p>I want keep the <code>RequestEntity</code> and <code>ResponseEntity</code> objects. Their API are fluent.</p>
  
  <p>Therefore what is the correct way?. I am with the impression an extra configuration in either client or server is need it in some place.</p>