修改http请求参数格式

最近迁移老项目(JAVA_WEB),由于老项目本身就是做的前后端分离;将老项目整改为新微服务,我想直接使用原来的前段页面;可是有一个问题,原来的页面ajax接口封装了一层,请求信息和现在借口不一致。于是有了这一篇

自定义HttpServletRequestWrapper处理参数:

package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

public class DemoHttpServletWrapper extends HttpServletRequestWrapper {

    private Map params;

    public DemoHttpServletWrapper(HttpServletRequest request) {
        super(request);
        String reqMessage = request.getParameter("req_message");
        JSONObject json = JSONObject.parseObject(reqMessage);
        Map eval = (Map) JSONPath.eval(json, "$.req_body");

        if (CollectionUtils.isEmpty(eval)) {
            params = new HashMap<>(0);
            return;
        }

        this.params = eval;

    }

    @Override
    public String getQueryString() {
        String collect = this.params.entrySet().stream().map(x -> x.getKey() + "=" + x.getValue()).collect(Collectors.joining("&"));
        System.out.println(collect);

        return super.getQueryString();
    }


    @Override
    public String getParameter(String name) {
        return this.params.get(name) == null? null : this.params.get(name).toString();
    }

    @Override
    public Map getParameterMap() {

        return getMapParams();
    }

    @Override
    public Enumeration getParameterNames() {
        return Collections.enumeration(this.params.keySet());
    }

    @Override
    public String[] getParameterValues(String name) {
        return getMapParams().get(name);
    }

    @Override
    public int getContentLength() {
      return getByteLength();
    }

    @Override
    public long getContentLengthLong() {
        return getByteLength();
    }

    private byte[] getByte(){
        return JSON.toJSONString(this.params).getBytes(Charset.forName("UTF-8"));
    }

    private int getByteLength(){
        return getByte().length;
    }


    private Map getMapParams(){
        Map map = new HashMap<>();
        for (Map.Entry entry : this.params.entrySet()) {
            String key = entry.getKey();
            Object value =  entry.getValue();
            if (entry.getValue() instanceof String[]) {
                map.put(key,(String[])value);
            }else{
                map.put(key,new String[]{value.toString()});
            }
        }
        return map;
    }


}

替换原有request

package com.example.demo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author cyh
 */
@WebFilter(urlPatterns = "/*")
public class ChangeFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        DemoHttpServletWrapper demoHttpServletWrapper = new DemoHttpServletWrapper((HttpServletRequest) servletRequest);
        filterChain.doFilter(demoHttpServletWrapper,servletResponse);
    }

}

开启servlet注解扫描 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan(value = "com.example.demo")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

控制层

package com.example.demo;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class DemoControll {

    @Autowired
    private DemoService demoService;

    @RequestMapping(value = "login")
    @ResponseBody
    public String login(@RequestParam Map map){

        return JSON.toJSONString(map);
    }
}

修改http请求参数格式_第1张图片

转载于:https://my.oschina.net/u/3484671/blog/3098023

你可能感兴趣的:(修改http请求参数格式)