@RequestParam接收参数值为null @RequestParam 接收参数的值为null的处理方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

@RequestParam接收参数值为null @RequestParam 接收参数的值为null的处理方式

Qyq0498   2021-11-05 我要评论
想了解@RequestParam 接收参数的值为null的处理方式的相关内容吗,Qyq0498在本文为您仔细讲解@RequestParam接收参数值为null的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:@RequestParam参数,接收参数值为null,@RequestParam值为null,下面大家一起来学习吧。

@RequestParam 接收参数的值为null

@RequestMapping(value = "/test")
 public String test( @RequestParam(value = "profit",required = false,defaultValue = "0") int profit){
      System.out.println("profit:"+profit);
       return "success";
}

第一种处理方式(如上图):defaultValue请求参数的默认值,一般和 required = false 一起使用

第二种处理方式:接收的参数如果是null的话,int就要改为Integer,Integer默认值为null

@RequestMapping(value = "/test")
 public String test(@RequestParam(
 @RequestParam(value = "profit",required = false) Integer profit){
      System.out.println("profit:"+profit);
       return "success";
}

对于@RequestParam的一些小疑问

问题一

首先我在使用spring时一直使用@RequestParam来校验参数是否为空,但是我想我对@RequestParam的用法产出了一些误解。

简单来说@RequestParam只能验证你有没有传这个参数,而不能验证你传的参数是否为空。

问题二

还有一个问题,有一次我写一个代码如下:method(@RequestParam User user),然后报错如下:Required XXX parameter 'xxx' is not present

把@RequestParam删除就行了

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们