Translating the error messages

It is really good for a developer to see the exceptions thrown by the application. However, for our users, they bear little value. We will therefore translate them. In order to do that, we have to inject our application's MessageSource class into our controller's constructor:

private final MessageSource messageSource;

@Autowired
public PictureUploadController(PictureUploadProperties uploadProperties, MessageSource messageSource) {
    picturesDir = uploadProperties.getUploadPath();
    anonymousPicture = uploadProperties.getAnonymousPicture();
    this.messageSource = messageSource;
}

Now, we can retrieve messages from our messages bundle:

@ExceptionHandler(IOException.class)
public ModelAndView handleIOException(Locale locale ...

Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.