2

I am working with Spring Framework 4.3.2

About Rest for POST and PUT the following situation happens.

For POST I have the following:

@PostMapping(consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Void> saveOne(@Validated @RequestBody Persona persona){

Observe the method has declared @Validated.

I need do the validation and return the error text message according the Locale sent it by the client/user.

I can do it through Spring MVC Test, for that I have the following:

resultActions = mockMvc.perform(post(uri).contentType(MediaType.APPLICATION_XML)
                     .accept(MediaType.APPLICATION_XML)
                     .locale(locale)
                     .content(...content...))).andDo(print());

Observe the .locale(locale) part.

Until here about JUnit through @Parameters I am able to send a list of Locales and thus see the error message in different languages. Until here all go how is expected

Now, the other way to access a Rest controller is through RestTemplate

I have the following:

RequestEntity<Persona> requestEntity = RequestEntity.post(uri)
               .contentType(MediaType.APPLICATION_JSON_UTF8)
               .accept(MediaType.APPLICATION_JSON_UTF8)                                                               
               .body(personaValid);

....

ResponseEntity<GenericCollection<ValidatedFieldErrorReport>> responseEntity_ 
  = restTemplate.exchange(requestEntity, parameterizedTypeReference);

Sadly RequestEntity through the post does not support .locale(locale)

Even If I add .header("Accept-Language", locale.toString()) it does not work, same consideration for .header("locale", locale.toString()).

Note: I can confirm when I print the RequestEntity object it sends the expected Locale but in the server: it ignores the 'locale' sent it (how if it never has been not sent it from the beginning) and uses the default Locale available in the server. I am confused with this approach.

I want keep the RequestEntity and ResponseEntity objects. Their API are fluent.

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.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
  • Which locale resolver do you have configured on the server side? – a better oliver Sep 19 '16 at 15:23
  • I only have defined how `@Bean`s a `LocaleChangeInterceptor` and `SessionLocaleResolver` with `new Locale("en","US")` and `registry.addInterceptor(localeChangeInterceptorConfig.localeChangeInterceptor())`. Seems missing a `@Bean` according with your question – Manuel Jordan Sep 19 '16 at 15:30
  • So your resolver always returns `new Locale("en","US")`? And you wonder why sending an `Accept-Language` header has no effect? – a better oliver Sep 20 '16 at 06:39
  • The server prints always just `en`. Seems I need define a `@Bean` in the server side. – Manuel Jordan Sep 20 '16 at 12:47
  • It seems I wasn't explicit enough. You get `en` because you have `new Locale("en","US")`. Choose an appropriate locale resolver. – a better oliver Sep 20 '16 at 13:00
  • I am going to check the API, I remember two, one for `Cookie` and `Session` I will check if there are more... – Manuel Jordan Sep 20 '16 at 13:16

1 Answers1

3

For RequestEntity you should use:

.header(HttpHeaders.ACCEPT_LANGUAGE, locale.toLanguageTag());
Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47
Miao Ai
  • 31
  • 3