jjzjj

Controller层接收前端传参的几种方法。@RequestParam、@RequestBody、@PathVariable。及参数校验。

努力努力,努力努力 2023-04-05 原文

一、@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;

有关Controller层接收前端传参的几种方法。@RequestParam、@RequestBody、@PathVariable。及参数校验。的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  6. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  7. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  8. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  9. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  10. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

随机推荐