前后端的传参与接参

前后端的传参与接参:

前端传参:

1、url传参

  1. ?传参
    http(s): //域名/项目名/接口名?key1=value1&key2=value2

  2. restful风格
    http(s): //域名/项目名/接口名/value1/value2(这种方法传的参数不是键值对)

2、表单传参

  1. action : 传参的地址

  2. method : 传参的方式,get/post

  3. enctype: 上传文件的格式 multipart/form-data

3、Json传参

后端接参:

1、直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式。

@RestController
@RequestMapping("/test")
public class testController {
    @RequestMapping("/login")
    public String login(String username,String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

2、通过HttpServletRequest接收,适用于GET 和 POST请求方式。

@RestController
@RequestMapping("/test")
public class testController {
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
    	String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

3、用注解@RequestParam绑定请求参数到方法入参,适用于GET 和 POST请求方式。

@RestController
@RequestMapping("/test")
public class testController {
    @RequestMapping("/login")
    public String login(@RequestParam("username") String username,@RequestParam("password") String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

4、用注解@PathVariable接受rest风格请求参数到方法入参 ,适用于GET 和 POST请求方式。

@RestController
@RequestMapping("/test")
public class testController {
    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username") String username,@PathVariable("password") String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "success";
    }
}

5、用注解@RequestBody绑定请求参数到方法入参 ,用于POST请求。

@RestController
@RequestMapping("/test")
public class InnerController {
 
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(@RequestBody User user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "success";
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值