Jetty 响应字符编码

Jetty response character encoding

如何在我对 UTF-8 的响应中设置默认字符编码?

我试过了

    System.setProperty("file.encoding", "UTF-8");

还有这个

    System.setProperty("org.eclipse.jetty.util.UrlEncoding.charset", "utf-8");

两者都没有任何效果 - 响应仍然与 header

一起发送
Content-Type: text/html; charset=ISO-8859-1

我想对所有 text/html 响应执行此操作,最好是在代码中而不是 XML。我正在使用 Jetty 9。

Jetty 文档声称它默认使用 UTF-8,但这似乎是个谎言。如果你做的是正常的response.getWrite().println("Hello"),那么content encoding是这样确定的

  1. 从内容类型到内容编码的默认映射是从 org/eclipse/jetty/http/encoding.properties:
  2. 加载的
        // MimeTypes.java:155
        ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding");
        Enumeration<String> i = encoding.getKeys();
        while(i.hasMoreElements())
        {
            String type = i.nextElement();
            __encodings.put(type,encoding.getString(type));
        }

默认文件是:

text/html   = ISO-8859-1
text/plain  = ISO-8859-1
text/xml    = UTF-8
text/json   = UTF-8
  1. Response.getWriter() 尝试使用该映射,但默认为 ISO-8859-1
@Override
public PrintWriter getWriter() throws IOException
{
    if (_outputType == OutputType.STREAM)
        throw new IllegalStateException("STREAM");

    if (_outputType == OutputType.NONE)
    {
        /* get encoding from Content-Type header */
        String encoding = _characterEncoding;
        if (encoding == null)
        {
            encoding = MimeTypes.inferCharsetFromContentType(_contentType);
            if (encoding == null)
                encoding = StringUtil.__ISO_8859_1;
            setCharacterEncoding(encoding);
        }

所以您可以看到 text/html 它没有默认为 UTF-8。我认为没有办法从代码更改默认值。您能做的最好的事情就是将 encoding.properties 文件更改为:

text/html   = UTF-8
text/plain  = UTF-8
text/xml    = UTF-8
text/json   = UTF-8

但即便如此,如果它发现其中没有的编码,它将默认为 ISO-8859-1。

response.setCharacterEncoding("UTF-8");

使用 Writer() 很重要;

对我来说 如果我写

resp.getWriter().println("Return");
resp.setContentType("text/html; charset=UTF-8");

我不会工作

但是如果我改变顺序

resp.setContentType("text/html; charset=UTF-8");
resp.getWriter().println("Return");

会好的

我为一个遗留应用程序创建了字符编码过滤器。

public class CharacterEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        if(req instanceof Request){             
            req.setCharacterEncoding("UTF-8");
        }
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}

在 web.xml 中,过滤器映射具有 /* 的 url 模式。这通过 CharacterEncodingFilter 路由来自 Web 应用程序的所有请求。

<filter>
    <display-name>CharacterEncoding</display-name>
    <filter-name>CharacterEncoding</filter-name>
    <filter-class>my.app.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

例如,您可以将默认的 UTF-8 字符集更改为 ISO-8859-1。 9.3以后的版本,文档没有说清楚哪个参数名。 在 9.3 之前是 org.eclipse.jetty.util.URI.charset 对于新版本,它已更改为 org.eclipse.jetty.util.UrlEncoding.charset 这是一个例子:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.4.15.v20190215</version>
    <configuration>
        <systemPropertiesFile>src/main/config/jetty/encode.properties</systemPropertiesFile>
        <jettyXml>src/main/config/jetty/jetty-env.xml</jettyXml>
    </configuration>    
</plugin>

encode.properties

的内容
org.eclipse.jetty.util.UrlEncoding.charset=ISO-8859-1