springboot获取web请求参数

主要内容: 对常用的注解进行解释说明,并用demo演示具体应用。

参考文章

springboot获取web请求参数

springboot注解
  1. 获取请求参数数据 @RequestParam

    这个注解主要用在获取url中的参数,可以用在Post中,但是建议只在GET请求中使用

@RequestMapping(path = "register", method = RequestMethod.GET)
    public JsonBuilder userRegister(@RequestParam(value = "username") String username,
                                    @RequestParam(value = "password") String password,
                                    @RequestParam(value = "phone") String phone,
                                    @RequestParam(value = "nickname") String nickname,
                                    HttpServletRequest request){

        // 从数据库查询有没有此用户,有的话返回错误,没有继续
        // xxx 各种操作
}

不管是 get还是post url中的参数 @RequestParam都可以获取,但是不能获取 请求体 中的数据.

  1. 获取请求体内容(body)数据 @RequestBody ()
    get没有请求体,主要用在post
    /**
     * 获取字符串内容
     * @param list
     * @return
     */
    @PostMapping("withBody1")
    public String withBody1(@RequestBody String body){
        return "获取请求内容数据1:"+body;
    }
    /**
     * 获取MAP对象
     * @param list
     * @return
     */
    @PostMapping("withBody3")
    public String withBody3(@RequestBody Map<String, String> body){
        return "获取请求内容数据3:"+body;
    }
    /**
     * 获取实体
     * @param list
     * @return
     */
    @PostMapping("withBody4")
    public String withBody4(@RequestBody UserInfoModel body){
        return "获取请求内容数据4:"+body;
    }
    /**
     * 获取列表
     * @param list
     * @return
     */
    @PostMapping("withBody6")
    public String withBody6(@RequestBody List<Map<String, Object>> list){
        return "获取请求内容数据6:"+list.toArray().toString();
    }
  1. 获取路径参数数据 @PathVariable
    在请求路径中有动态参数时,可以用pathvariable获取
    @GetMapping("withPath/{userId}/info")
    public String withPath(@PathVariable String userId){
        return "获取请求路径参数"+userId;
    }
  1. 获取header数据 @RequestHeader
    获取请求头中的数据,token等等
    @GetMapping("withHeader")
    public String withHeader(@RequestHeader String token){
        return "获取请求头参数"+token;
    }
  1. 获取cookie数据 @CookieValue
    ** 获取前端cookie中的数据**
    @GetMapping("withCookie")
    public String withCookie(@CookieValue String token){
        return "获取cookie参数"+token;
    }
  1. 获取FORM表单参数
/**
     * 获取FORM表单数据  通过参数
     * @param formKey1
     * @param formKey2
     * @return
     */
    @PostMapping("withForm1")
    public String withForm1(@RequestParam String formKey1,@RequestParam String formKey2){
        return "获取FORM表单参数formKey1:"+formKey1+" :formKey2:"+formKey2;
    }
    
    /**
     * 获取FORM表单数据 通过对象
     * @param userInfo
     * @return
     */
    @PostMapping("withForm2")
    public String withForm2(@ModelAttribute UserInfoModel userInfo){
        return "获取FORM表单参数:"+userInfo.toString();
    }
  1. 通用方法,可以获取到所有类型的参数(上面所有都可以获取
    @PostMapping("withRequest")
    public String withRequest(HttpServletRequest request) throws IOException{
        // 获取请求参数
        request.getParameter("");
        // 获取请求头
        request.getHeader("");
        // 获取cookie
        request.getCookies();
        // 获取路径信息 
        request.getPathInfo();
        // 获取body流
        request.getInputStream();
        return "其实上面讲了那么多,这个才是大BOOS";
    }
文件上传
    /**
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/image")
    public String uploadImage(@RequestParam MultipartFile file) throws IOException{
        String contentType = file.getContentType();
        if (contentType.equals(MediaType.IMAGE_JPEG_VALUE)
                ||contentType.equals(MediaType.IMAGE_PNG_VALUE)) {
            // 获取文件流
            InputStream is= file.getInputStream();
            byte[] data = new byte[2048];
            FileOutputStream fis = new FileOutputStream(
                    new File("D:\\liuawei\\springbootbucket\\resources\\upload\\"+file.getOriginalFilename()));
            while(is.read(data)!=-1){
                fis.write(data);
            }
            fis.flush();
            fis.close();
            is.close();
        }else {
            return "error";
        }
        return "success";
    }   
参数校验
  1. 添加参数校验框架依赖
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.13.Final</version>
</dependency>
  1. 在JavaBean上面配置检验规则
    @Min(value=15,message="最小值是15")
    @Max(value=130,message="最大值是130")
    private int age;
    
    @Email(message="必须符合邮件地址")
    private String email;
    
    @Past(message="日期是过去的日期")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date birthday;
    
    @NotBlank
    private String remark;
  1. 在请求方法添加@Valid进行校验
    @GetMapping("validata")
    public String validata(@Valid ValBean bean,BindingResult result){
        if (result.hasErrors()) {
            return "参数校验失败";
        }
        return "参数校验成功";
    }
springboot学习系列文章

springboot学习系列文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值