一、@RequestParam

主要用于将请求参数区域的数据映射到控制层方法的参数上

// http://localhost:8080/wh/user/edit?Id=9452659856325148452&name=天天向上
// @RequestParam源码

@Target({ElementType.PARAMETER}) // 只能作用于参数上
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
     // 定义参数名称,默认和名字一致
    @AliasFor("name")
    String value() default "";
    // 定义参数名称,默认和名字一致
    @AliasFor("value")
    String name() default "";
    // 默认必填,一旦加上该注解,前台必须传递此参数
    boolean required() default true;
    // 定义默认值
    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

1、@RequestParam有三个配置参数:
required        表示是否必传,默认为 true(可省略不写)。
defaultValue        可设置请求参数的默认值(可省略不写)。
value        为接收url的参数名(一般与接收参数名相同)。

小知识点:@AliasFor是一个注解,用于为注解属性声明别名。上方源码有两个属性value和name 被@AliasFor注解注释了自身,并且value和name 互为别名。

2、@RequestParam加与不加的区别:

    // userType非必传
    @GetMapping("/userList1")
    @ApiOperation(value = "获取所有账号")
    public ApiResult<List<SysUserVO>> getUserList(Integer userType) {
        return ApiResult.data(userService.getUserList(userType));
    }

    // userType非必传
    @GetMapping("/userList2")
    @ApiOperation(value = "获取所有账号")
    public ApiResult<List<SysUserVO>> getUserList(@RequestParam(value = "userType", required = false) Integer userType) {
        return ApiResult.data(userService.getUserList(userType));
    }

    // userType必传
    @GetMapping("/userList3")
    @ApiOperation(value = "获取所有账号")
    // 或者参数前也可以只加@RequestParam,默认必传
    public ApiResult<List<SysUserVO>> getUserList(@RequestParam(value = "userType", required = true) Integer userType) {
        return ApiResult.data(userService.getUserList(userType));
    }

    // userType必传(@RequestParam默认required=true)
    @GetMapping("/userList4")
    @ApiOperation(value = "获取所有账号")
    public ApiResult<List<SysUserVO>> getUserList(@RequestParam Integer userType) {
        return ApiResult.data(userService.getUserList(userType));
    }
1不加@RequestParam前端的参数名需要和后端控制器的变量名保持一致才能生效
2不加@RequestParam参数为非必传,加@RequestParam写法参数为必传。但@RequestParam可以通过@RequestParam(required = false)设置为非必传。
3@RequestParam可以通过@RequestParam(“userId”)或者@RequestParam(value = “userId”)指定传入的参数名。
4@RequestParam可以通过@RequestParam(defaultValue = “0”)指定参数默认值
5如果接口除了前端调用还有后端RPC调用,则不能省略@RequestParam,否则RPC会找不到参数报错
6

不加@RequestParam注解,或@RequestParam注解required=false:url可带参数也可不带参数,输入 localhost:8080/userList1 以及 localhost:8080/userList1 ?userType=xxx 方法都能执行


加@RequestParam注解:url必须带有参数。也就是说你直接输入localhost:8080/userList3 会报错,不会执行方法。只能输入localhost:8080/userList3?userType=xxx 才能执行相应的方法

 3、举些栗子:@RequestParam与@PathVariable一起用,以及一些参数校验注解

    // id必传,且用@NotNull注解不能为null,keyWord非必传
    @GetMapping("/catalogue/{id}")
	public ApiResult<List<LibraryVO>> selectFilePage(@ApiParam(value = "目录ID(根目录:固定值'0')", required = true) @NotNull(message = "请选择目录") @PathVariable Long id,
													 @ApiParam(value = "搜索关键词") @RequestParam(required = false) String keyWord) {
		return ApiResult.data(null);
	}

    //两个参数都必传,用到了@NotNUll、@Length、@Pattern参数校验注解,在文末有讲解
	@PutMapping("/refuse")
	public ApiResult<Boolean> refuse(@NotNull(message = "订单ID不能为空") @RequestParam("id") Long id,
									 @NotNull(message = "退回原因不能为空")
									 @Length(max = 200, message = "退回原因最多允许输入200个字符")
									 @Pattern(regexp = "^[\\u4E00-\\u9FA5A-Za-z0-9\\s\\u00a0\\u3000`~!@#$%^&*()+=_|{}':;',\\\\\\[\\].<>/?~!@#¥%……&*()——+||{}【】‘;:”“’\"。,、?《》「」¥~-]*$", message = "退回原因不允许输入特殊字符")
									 @RequestParam("reason") String reason) {
		return ApiResult.data(null);
	}

二、@PathVariable

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。

// http://localhost:8080/wh/user/edit/9536258451259455114
// 源码
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
    @AliasFor("name")
    String value() default "";

    @AliasFor("value")
    String name() default "";

    boolean required() default true;
}

1、required默认必传。 

2、举个栗子

    // @PathVariable可以用来映射URL中的占位符到目标方法的参数中
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "系统用户查看详情", notes = "传入userId")
    public ApiResult<SysUserDetailVO> detail(@ApiParam(value = "主键", required = true) @PathVariable Long id) {
        return ApiResult.data(userService.detail(id));
    }

    // 两个参数都必传	
    @GetMapping("/detail/{applyId}/{source}")
	public ApiResult<ContractReviewApplyVO> detail(@ApiParam(value = "审批流id", required = true) @PathVariable("applyId") Long applyId,
                                                   @ApiParam(value = "来源:1->业务审批;2->评审信息;3->续约信息", required = true) @PathVariable("source") Integer source){
		return ApiResult.data(contractReviewApplyService.detail(applyId,source));
	}

    // 两个参数都必传,且都不能为空
    @PostMapping("/customer/rename/{customerId}")
    public ApiResult customersRename(@Valid @RequestBody @NotEmpty(message = "参数不能为空") List<CustomerRenameDTO> renameList, 
                                     @NotNull(message = "客户id不能为空") @PathVariable("customerId") Long customerId) {
        return ApiResult.status(customerService.customersRename(renameList,customerId));
    }

三、@RequsetBody

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。

    @PostMapping("/")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增或编辑", notes = "")
    public ApiResult<Boolean> saveOrUpdate(@RequestBody List<WorkbenchOftenFeaturesRecordDTO> list) {
        return ApiResult.status(true);
    }

    // @RequestBody与@RequestParam联用
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "简历草稿记录表新增", notes = "传入ResumeImportVO")
    public ApiResult<String> save(@RequestParam(value = "id", required = false) Long id, @RequestBody ErpResume erpResume) {
        return ApiResult.data(null);
    }

    // @RequestBody与@PathVariable联用
    @PostMapping("/submit/{queryType}/{type}")
    public ApiResult<String> submit(@Valid @RequestBody ErpResume erpResume,
                                     @ApiParam(value = "简历类型:1-公共简历,2-个人简历", required = true) @PathVariable Integer queryType,
                                     @ApiParam(value = "按钮类型:1->上传简历;2->编辑;3->批量上传保存简历;4->保存简历后编辑简历;5->覆盖原简历", required = true) @PathVariable Integer type) {
        return ApiResult.data(null);
    }

使用@RequestBody时注意

1. 不支持get请求,因为get请求没有HttpEntity
2. 必须要在请求头中申明content-Type(如application/json).springMvc通过HandlerAdapter配置的HttpMessageConverters解析httpEntity的数据,并绑定到相应的bean上
3. 在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
4. 建议最好不要与@RequestParam一起使用,是因为@RequestBody会将InputStream吃掉,造成后面的@RequsetParam无法匹配到参数而报400

四、接口参数校验,部分常用注解
@NotEmpty 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@NotBlank 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@Max(value) 限制必须为一个不大于指定值的数字
@Min(value) 限制必须为一个不小于指定值的数字
@Pattern(value) 限制必须符合指定的正则表达式
@Size(max,min) 限制字符长度必须在min到max之间
@Length 验证字符串长度是否在给定的范围之内
@Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
@ApiParam是用于swagger提供开发者文档,文档中生成的注释内容。value:参数描述;
defaultValue:参数默认值;required:是否为必传参数,默认为false。
@Valid 注解通常用于对象属性字段的规则检测,适用于基本数据类型(Integer,Long,Double等等),如下:
@NotNull: 注解被使用在 String 类型的数据上,表示该数据不能为 Null(但是可以为 Empty);
@NotBlank:适用于 String 类型的数据上,加了@NotBlank 注解的参数不能为 Null 且 trim() 之后 size > 0;
@NotEmpty:适用于 String、Collection集合、Map、数组等等,加了@NotEmpty 注解的参数不能为 Null 或者 长度为 0;

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐