tomcat 部署swagger 请求到后端乱码

问题:

 @ApiOperation(value = "", notes = "查看关键词列表")
@ResponseBody
@RequestMapping(value = "getByKeywords", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
Result<Page<List<AplQuestionAnswer>>> listQuestionAndAnswers(
@ApiParam(required = true, name = "keywords", value = "关键词") @RequestParam(value = "keywords", required = true) String[] keywords,
@ApiParam(required = false, name = "pageSize", value = "页大小", defaultValue = "20") @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize,
@ApiParam(required = false, name = "pageIndex", value = "当前页", defaultValue = "0") @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex
) { //如果为空则查询全部
if (keywords == null || keywords.length == 0) {
keywords = new String[]{};
}

前端通过swagger的方式进行获取:(注意数组在swagger中直接用回车进行获取)

问题定位:

  平台码为iso-8859-1造成的,在拿到是开之后先进行解码并转码。

最终解决方式:

public static String decodeAndTrim(String str) throws UnsupportedEncodingException {
if (str == null) {
return null;
}
String encoding = TemplateStringUtil.getEncoding(str);
if ("ISO-8859-1".equals(encoding)) {
str = new String(str.getBytes("ISO-8859-1"), "utf-8");
}
return str.trim();
}

尝试过的方案:(同事通过这种方式解决了,但是我这里没有)

在tomcat 的JVM option中增加

-Dfile.encoding=UTF-8

和tomcat中的配置

    <Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /> 中增加 URIEncoding="UTF-8" <Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
上一篇:jQuery选择器模糊匹配


下一篇:iOS即时通讯之CocoaAsyncSocket源码解析三