@RestController
@SpringBootApplication
public class DemoApplication {
@RequestMapping("/")
String home() {
throw new ResponseStatusException(BAD_REQUEST, "bad.request");
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"spring.mvc.problemdetails.enabled=true"})
class ProblemDetailsTests {
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private MessageSource messageSource;
@Test
void detailShouldBeLocalized() {
ResponseEntity<Map<String, Object>> resp = testRestTemplate.exchange(RequestEntity.method(GET, "/").build(),
new ParameterizedTypeReference<Map<String, Object>>() {
});
assertThat(resp.getBody().get("detail")).isEqualTo(messageSource.getMessage("bad.request",null,null)); // failed
}
}
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
class ErrorAttributesTests {
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private MessageSource messageSource;
@Test
void messageShouldBeLocalized() {
ResponseEntity<Map<String, Object>> resp = testRestTemplate.exchange(RequestEntity.method(GET, "/").build(),
new ParameterizedTypeReference<Map<String, Object>>() {
});
assertThat(resp.getBody().get("message")).isEqualTo(messageSource.getMessage("bad.request",null,null)); // passed
}
}
detail should be localized like traditional message.
Here is test project demo.zip
Comment From: wilkinsona
Localisation of problem details is handled by Spring Framework and, therefore, is out of Spring Boot's control. As you have seen it doesn't localise the reason. Instead, it uses the message detail code. I believe that this will work:
throw new ResponseStatusException(BAD_REQUEST, "bad.request") {
@Override
public String getDetailMessageCode() {
return "bad.request";
}
};
If you think that the localisation support could be improved, please open a Spring Framework issue.