Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设置值时,使用默认值设置RequestParam始终为空_Java_Spring_Rest - Fatal编程技术网

Java 设置值时,使用默认值设置RequestParam始终为空

Java 设置值时,使用默认值设置RequestParam始终为空,java,spring,rest,Java,Spring,Rest,我正在创建一个rest方法,在本例中,它可以使用RequestParam注释处理可选的parametergender。但性别值始终被视为空值。不过,认为spring版本4.0.0秒参数是正确的 样本: 提前谢谢。 Pankaj就像朱纳德在评论中说的那样,也使用@PathVariable表示性别 public @ResponseBody String getByAge(@PathVariable("gender") String gender, @PathVariable("age") int a

我正在创建一个rest方法,在本例中,它可以使用RequestParam注释处理可选的parametergender。但性别值始终被视为空值。不过,认为spring版本4.0.0秒参数是正确的

样本:

提前谢谢。
Pankaj

就像朱纳德在评论中说的那样,也使用@PathVariable表示性别

public @ResponseBody String getByAge(@PathVariable("gender") String gender, @PathVariable("age") int age) throws Exception {...}
若您想要可选参数,并且若您有请求参数,那个么您的url应该类似于http://localhost:8080/bll-0.1/qry/age/40?性别=女性,您可以使用@RequestParam


为什么你使用RequestParam而不是PathParam来表示性别?你有一个PathVariable,不是RequestParam。我在你的示例URL中没有看到任何请求参数,应该是这样的,有一个RequestParam谢谢Babl。我将性别从Pathvariable更改为RequestParam。并且仍然坚持旧的质疑。谢谢。但是我想选择性别。PathVariable没有给我那种自由。我将其从Pathvariable更改为@RequestParam,并为此添加了一个新的requestmapping,但忘记更改查询。谢谢。您可以有两种方法:一种是同时使用param,另一种是仅使用一个param。
public @ResponseBody String getByAge(@PathVariable("gender") String gender, @PathVariable("age") int age) throws Exception {...}
public @ResponseBody String getByAge(@RequestParam(value="gender", defaultValue="unknown") String gender, @PathVariable("age") int age) throws Exception {...}