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

     

     

  • 相关阅读:
    CS231n assignment3 Q1 Image Captioning with Vanilla RNNs
    使用tensorflow预测函数的参数值(a simple task)
    CS231n assignment2 Q5 TensorFlow on CIFAR-10
    CS231n assignment2 Q4 Convolutional Networks
    HDU 1561 The more, The Better
    HDU4003 Find Metal Mineral
    poj 1947 Rebuilding Roads
    2090 背包
    poj 2408 Apple Tree
    奔跑的xiaodao
  • 原文地址:https://www.cnblogs.com/zhangshiwen/p/7448093.html
Copyright © 2020-2023  润新知