Reputation: 843
I'm mounting a directory which has translations inside of JavaScript. In order for the browser to render them properly, I need to pass a character encoding header.
The directory is mounted through Spring's resource handler like so:
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ResourceLoaderAware {
@Override
public void addResourceHandlers(@Nonnull final ResourceHandlerRegistry registry) { registry.addResourceHandler("/app/static/scripts/**")
.addResourceLocations("/static/scripts/")
.setCachePeriod((int) TimeUnit.DAYS.toSeconds(365));
...
}
...
}
I haven't found an obvious way to set the default character encoding to UTF-8. Any ideas? I'm on Spring MVC 3.2.4.
Upvotes: 0
Views: 1234
Reputation: 843
Though Interceptors don't get called when requesting resources, filters do. All I had to do was add a filter that set the response's character encoding to UTF-8.
/**
* Sets character encoding on the request and response.
*
* @author gaurav
*/
public class CharacterEncodingFilter implements Filter {
@Override
public void init(final FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
response.setCharacterEncoding("UTF-8");
} finally {
chain.doFilter(request, response);
}
}
@Override
public void destroy() { }
}
Upvotes: 2