我试图确保来自基于泽西的java应用程序的所有JSON响应都有一个UTF-8字符编码参数附加到其ContentType头。
因此,如果这是一个JSON响应,我希望Content-Type
的响应头是
内容类型:application/json;字符集=UTF-8
EDIT:我知道我可以根据具体情况进行编辑,但我希望在全局范围内进行编辑,因此它会影响所有内容类型为“application/json”的内容响应代码>
如果我只是尝试在我的过滤器中设置字符编码,而不考虑内容类型,它就可以正常工作。但我只想在ContentType为“application/json”时设置字符编码。我发现答案是肯定的。getContentType()方法始终返回null,除非调用chain。先过滤。但是如果我尝试在这之后更改字符编码,它似乎总是被覆盖。
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ws.rs.core.MediaType;
public class EnsureJsonResponseIsUtf8Filter implements Filter
{
private class SimpleWrapper extends HttpServletResponseWrapper
{
public SimpleWrapper(HttpServletResponse response)
{
super(response);
}
@Override
public String getCharacterEncoding()
{
return "UTF-8";
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
chain.doFilter(request, response);
if (response.getContentType() != null && response.getContentType().contains(MediaType.APPLICATION_JSON))
{
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, new SimpleWrapper((HttpServletResponse) response));
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
}
@Override
public void destroy()
{
}
}
我见过其他类似的问题,但似乎没有一个有这个问题。我已尝试将我的筛选器注册为第一个和最后一个筛选器,但没有成功。
多亏了这一页上的其他答案,我找到了一种方法。。。。非常接近他们的建议,但事实证明,我能让它工作的唯一方法是重写“getOutputStream”,并在此时查看contentType。我将此过滤器作为链中的第一个过滤器,它似乎工作正常。
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ws.rs.core.MediaType;
public class EnsureJsonIsUtf8ResponseFilter implements Filter
{
final String APPLICATION_JSON_WITH_UTF8_CHARSET = MediaType.APPLICATION_JSON + ";charset=" + java.nio.charset.StandardCharsets.UTF_8;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletResponse r = (HttpServletResponse) response;
HttpServletResponse wrappedResponse = new HttpServletResponseWrapper(r)
{
@Override
public ServletOutputStream getOutputStream() throws java.io.IOException
{
ServletResponse response = this.getResponse();
String ct = (response != null) ? response.getContentType() : null;
if (ct != null && ct.toLowerCase().startsWith(MediaType.APPLICATION_JSON))
{
response.setContentType(APPLICATION_JSON_WITH_UTF8_CHARSET);
}
return super.getOutputStream();
}
};
chain.doFilter(request, wrappedResponse);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
// This method intentionally left blank
}
@Override
public void destroy()
{
// This method intentionally left blank
}
}
这样做是行不通的。
当您调用链时。doFilter(请求、响应)代码>标题已刷新,以后无法重置。
你能做的其实是一个快速而肮脏的把戏:
public void doFilter(...) {
HttpServletResponse resp = new HttpServletResponseWrapper(response) {
public void setContentType(String ct) {
if(ct!=null && ct.toLowerCase().startsWith("application/json")) {
super.setContentType("application/json;charset=UTF-8");
} else {
super.setContentType(ct);
}
}
}
// Set content type manually to override any potential defaults,
// See if you need it at all
response.setContentType("application/json;charset=UTF-8");
chain.doFilter(request, resp); // Inject our response!
}
编辑:ct.toUpperCase(). startsFor("应用程序/json")
改为ct.toLowerCase(). startsFor("应用程序/json")
。
使用此答案作为参考,您的问题的解决方案是重新编码JSON文本,如下所示:
public void doFilter(...) {
final CharResponseWrapper wrappedResponse =
new CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrappedResponse);
final String content = wrappedResponse.toString();
final String type = wrappedResponse.getContentType();
if (type != null && type.contains(MediaType.APPLICATION_JSON)) {
// Re-encode the JSON response as UTF-8.
response.setCharacterEncoding("UTF-8");
final OutputStream out = response.getOutputStream();
out.write(content.getBytes("UTF-8"));
out.close();
}
else {
// Otherwise just write it as-is.
final PrintWriter out = response.getWriter();
out.write(content);
out.close();
}
}