嵌入式码头服务器导致 UTF-8 字符出现问题
Embedded jetty server causing issues with UTF-8 characters
servlet 文件中的代码
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
try {
Map map = req.getParameterMap();
HashMap<String, Object> paramsMap = new HashMap<String, Object>(map);
String action = HTTPUtil.getInstance().getPropertyFromHttpRequest(PARAM_ACTION, paramsMap, false);
String service = HTTPUtil.getInstance().getPropertyFromHttpRequest(PARAM_SERVICE_NAME, paramsMap, false);
String requestBody = (String)IOUtils.toString(req.getInputStream());
....
requestBody 中的一个字段在 requestBody 中呈现为 "Brad?" 而不是 "Brad's"。理想情况下 "Brad's" 应该被渲染。这导致不正确的数据存储在数据库中。
码头服务器是这样启动的:
server = new Server(new QueuedThreadPool(10));
ServerConnector connector = new ServerConnector(server);
connector.setPort(getPort());
server.addConnector(connector);
HandlerCollection handlerCollection = getHandlers();
server.setHandler(handlerCollection);
server.setStopAtShutdown(true);
server.setStopTimeout(1000);
server.start();
getHandlers() return HandlerCollection 创建为:
HandlerCollection handlers = new HandlerCollection(true);
ServletContextHandler handler = new ServletContextHandler();
handler.getContextPath();
handler.addServlet(new ServletHolder(new TestServlet(), "/test");
handlers.addHandler(handler);
码头版本 9.2.11.v20150529
curl -X POST \
'http://master92.XYZ.com:7777/health?test=10&action=10' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json; charset=utf-8' \
-d '{"name":"ABA …"}'
请求正文显示为
{"name": "ABA ?" } 转储到日志或控制台时
问题已通过 -Dfile.encoding=UTF-8 选项解决。
servlet 文件中的代码
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
try {
Map map = req.getParameterMap();
HashMap<String, Object> paramsMap = new HashMap<String, Object>(map);
String action = HTTPUtil.getInstance().getPropertyFromHttpRequest(PARAM_ACTION, paramsMap, false);
String service = HTTPUtil.getInstance().getPropertyFromHttpRequest(PARAM_SERVICE_NAME, paramsMap, false);
String requestBody = (String)IOUtils.toString(req.getInputStream());
....
requestBody 中的一个字段在 requestBody 中呈现为 "Brad?" 而不是 "Brad's"。理想情况下 "Brad's" 应该被渲染。这导致不正确的数据存储在数据库中。
码头服务器是这样启动的:
server = new Server(new QueuedThreadPool(10));
ServerConnector connector = new ServerConnector(server);
connector.setPort(getPort());
server.addConnector(connector);
HandlerCollection handlerCollection = getHandlers();
server.setHandler(handlerCollection);
server.setStopAtShutdown(true);
server.setStopTimeout(1000);
server.start();
getHandlers() return HandlerCollection 创建为:
HandlerCollection handlers = new HandlerCollection(true);
ServletContextHandler handler = new ServletContextHandler();
handler.getContextPath();
handler.addServlet(new ServletHolder(new TestServlet(), "/test");
handlers.addHandler(handler);
码头版本 9.2.11.v20150529
curl -X POST \
'http://master92.XYZ.com:7777/health?test=10&action=10' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json; charset=utf-8' \
-d '{"name":"ABA …"}'
请求正文显示为
{"name": "ABA ?" } 转储到日志或控制台时
问题已通过 -Dfile.encoding=UTF-8 选项解决。