J'ai codé mon propre filtre de printemps afin de coder en UTF-8 toutes les réponses à l'exception des images:filtres personnalisés au printemps
package my.local.package.filter;
public class CharacterEncodingFilter extends org.springframework.web.filter.CharacterEncodingFilter
{
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException
{
if(!request.getRequestURI().endsWith("jpg") &&
!request.getRequestURI().endsWith("png") &&
!request.getRequestURI().endsWith("gif") &&
!request.getRequestURI().endsWith("ico"))
{
super.doFilterInternal(request, response, filterChain);
}
filterChain.doFilter(request, response);
}
}
Je fais référence dans le web.xml:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>my.local.package.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Tout fonctionne comme prévu, les fichiers jpg/png/gif/ico ne sont pas encodés en UTF-8 alors que tous les autres fichiers le sont.
J'essaie maintenant de coder un contrôleur simple qui doit retourner une erreur 404 dans certaines conditions:
@Controller
public class Avatar
{
@RequestMapping("/images/{width}x{height}/{subject}.jpg")
public void avatar(HttpServletResponse response,
@PathVariable("width") String width,
@PathVariable("height") String height,
@PathVariable("subject") String subject) throws IOException
{
...
// if(error)
// {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found");
return;
// }
...
}
}
Mais lorsque vous demandez, par exemple, obtenir un /images/52x52/1.jpg i la page contenant cette erreur:
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
Je pense que je le filtre codé une mauvaise façon (je ne suis pas connu avec le printemps), parce que si je précise org.springframework.web.filter.CharacterEncodingFilter
au lieu de my.local.package.filter.CharacterEncodingFilter
dans le fichier web.xml, il fonctionne parfaitement .
Quelqu'un peut-il m'aider?
Merci.
Merci beaucoup, cela a résolu le problème! – satoshi