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"/>

     

     

  • 相关阅读:
    bzoj 3670: [Noi2014]动物园
    bzoj 2878: [Noi2012]迷失游乐园
    51nod 1348 乘积之和
    51nod 1514 美妙的序列
    AtCoder Grand Contest 002 D
    bzoj 3451 Normal
    LOJ #6119. 「2017 山东二轮集训 Day7」国王
    51nod 1752 哈希统计
    计蒜客 百度地图的实时路况
    Codeforces 549F Yura and Developers
  • 原文地址:https://www.cnblogs.com/zhangshiwen/p/7448093.html
Copyright © 2011-2022 走看看