zoukankan      html  css  js  c++  java
  • 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"/>

     

     

  • 相关阅读:
    移动端1px问题
    js几种数组排序及sort的实现
    从零开始搭建vue移动端项目到上线
    Maven项目常见错误解决方法汇总
    重读《Java编程思想》
    ubuntu开发环境下eclipse的alt+/自动补全功能不能用
    Linux环境下解压rar文件
    Ubuntu 16.04下deb文件的安装
    优化Ubuntu 16.04系统的几件事
    Ubuntu16.04 安装 “宋体,微软雅黑,Consolas雅黑混合版编程字体” 等 Windows 7 下的字体
  • 原文地址:https://www.cnblogs.com/zhangshiwen/p/7448093.html
Copyright © 2011-2022 走看看