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 Locale
s 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.