SpringMvc adaptation Post and get requests to add Nginx reverse proxy

Embedded or mobile banking services through the project, which is relatively pit where no project documentation, there is no data exchange specifications are touching the stones. So the production after the discovery of a large Bug, IOS can not receive the requested data is! (I did not participate in the testing process, the testing process may not be suspected of IOS test)

After contact with the CCB head office mobile banking, mobile banking learned that due to the development framework restrictions, IOS can only send requests Post request, the request is divided into two types:
Android -- GET请求 Ios -- POST请求

13566833-580b5d964c76e705.jpg
Scenario Description .jpg

So we need to update the code, the two kinds of each adaptation request mode, wherein the read parameter for our use. Frame SpringMVC development, since the transmission parameters less, so before it considers only get request, takes parameters are very smooth, it is necessary to increase the post request to the code determination ,,:

String userinfo = null;
        if (request.getQueryString() != null) {
            String requestStr = request.getRequestURL() + "?" + request.getQueryString();
            log.info("Get请求=====" + requestStr);
            userinfo = request.getParameter("userInfo");
            
            if (userinfo != null && userinfo.length() > 0) {
                userinfo = userinfo.replaceAll(" ", "+");
                log.info("Get请求取到的userInfo的值为=====" + userinfo);
            } else {
                log.error("userInfo的值为null");
            }
            
        } else {
            StringBuffer sb = new StringBuffer();
            InputStream is;
            String requestStr = null;
            BufferedReader br = null;
            try {
                is = request.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String s = "";
                while ((s = br.readLine()) != null) {
                    sb.append(s);
                }
                requestStr = sb.toString();
                
                log.info("Post请求=====" + requestStr);
                
                String[] user = requestStr.split("&");
                
                for (int i = 0; i < user.length; i++) {
                    if(user[i].contains("userInfo")){
                        String[] userStrSub = user[i].split("=");
                        userinfo = userStrSub[1];
                        break;
                    }
                }
                
                if (userinfo != null && userinfo.length() > 0) {
                    userinfo = userinfo.replaceAll("%2B", "+");
                    log.info("Post请求取到的userInfo值为==" + userinfo);
                } else {
                    log.error("userInfo的值为null");
                }
            
            } catch (IOException e1) {
                e1.printStackTrace();   
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

The project springMvc frame, post and get requests received, @RequestMapping (value = "") when using the results of a post request sent Postman, Postman a '/' and without '/' results quite different

postMan send a POST request: HTTP: // localhost: 8080 / yndist
IO stream through the read request, the result is null

postMan send a POST request: HTTP: // localhost: 8080 / yndist /
by io stream read request, the result can take a string to json

Why add a '/' and without '/' results will be different? ? Use request.getRequestURI()the results to take is /yndist/, why print IO streams will result when large-diameter Aiba it?

这个有机会研究下,暂时不考虑这个因素。。。

Use packet capture tool, the results add '/' and without '/' have got the data, at this time, indicating that the program appeared somewhere Bug, but the reason why this occurs, temporarily I did not find. . . .

@RequestMapping (value = "") at the place to explain the reasons set null: the
head office providing server, the default listening port 8080, the https://zwfw.yn.gov.cn/yndistdefault points to the server's ip + port + yndist, is this: 127.0.0.1: 8080 / yndist, so this point is particularly similar path with WEB service, so this place is set up to NUll.

Therefore, according to this https://zwfw.yn.gov.cn/yndisthardcoded access url, I put the project name has also been changed yndist, but when receiving the post request, IO print stream is null, no matter how are to get any value, use postMan test, there have been plus '/' and without '/' distinction.

Think of any other way to use Nginx to do a reverse proxy configuration is particularly simple:

server {
        listen       8080;
        server_name  127.0.0.1; //如有需要,需更改为自己服务器Ip

        #access_log  logs/host.access.log  main;
        
        location /yndist {
                # 转发请求到后端服务网关
                proxy_pass http://127.0.0.1:7070/yndist/index.do;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|properties|json)$ {
            proxy_pass http://127.0.0.1:7070;
        }
   }

Then change the Tomcat default port 7070, @ @RequestMapping (value = "/ index.do "), then add intercept, debugging OK!

In fact, this place can not Nginx, Tomcat can set up a reverse proxy, but several tried, without success, there are known methods can be under the guidance of the great God, so using Nginx friends. .

Reproduced in: https: //www.jianshu.com/p/b579f1b44f40

Guess you like

Origin blog.csdn.net/weixin_34415923/article/details/91105268