20201207-spring学习

一:controller层请求参数整理

@RequestParam

接收的是?后面拼接的参数;
注: 被@RequestParam修饰的参数是必填的,如下面的例子:
http://localhost:9001/test?name=123&pass=456 ▶▶▶ ✔
http://localhost:9001/test?name=&pass=456 ▶▶▶ ✔
http://localhost:9001/test?name=123 ▶▶▶ ✔
http://localhost:9001/test?pass=456 ▶▶▶ ✘

http://localhost:9001/test?name=123&pass=456
// 后台接收:
 @GetMapping(value = "/test")
public void test(@RequestParam String name, String pass) {
     System.out.println("*************name***********"+name);
     System.out.println("*************pass***********"+pass);
 }

@PathVariable

使用@PathVariable接收参数,参数值需要在url进行占位

http://localhost:9001/test/123
// 后台接收:
 @GetMapping(value = "/test/{name}")
public void test(@PathVariable String name) {
     System.out.println("*************name***********"+name);
 }

@RequestBody

使用@RequestBody接收参数,前端请求方式必须是json格式;
即: Content-type:application/json

    @PostMapping(value = "/payment/create")
    public CommonResult save(@RequestBody Payment payment) {
        int result = paymentService.insert(payment);
        log.info("*****插入结果:" + result);
        if (result > 0) {
            return new CommonResult<>(200, "插入数据库成功", result);
        } else {
            return new CommonResult<>(444, "插入数据库失败");
        }
    }

@RequestAttribute

@RequestAttribute修饰的参数主要是从request中获取属性参数;
基本应用是从请求转发过来的,或者是filter接收处理了一下参数;

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    final HttpServletRequest req = (HttpServletRequest) servletRequest;
    final String path = req.getServletPath();
    if (StringUtils.isBlank(path )) {
        final String[] array = path.split("\\.");
        if (array.length > 1) {
            //登录请求,过滤
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            try {
                getPostData(req);
            } catch (Exception e) {
                e.printStackTrace();
            }
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}
//**********************getPostData 大致意思**************
//1:  先对请求路径进行过滤
final String path = req.getServletPath();
//2: 针对token进行认证
final Map<String, String> header = getHeadersInfo(req);
final String token = header.get("authorization");
//3: 统一拿到参数,放到postData中
String postData = req.getParameter("postData");
//4: 放行
//**********************后台接收(达到统一接收目的)**************
 @RequestMapping("/handleLogins")
public Map<String, Object> handleLogins(@RequestAttribute String postData);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值