解决方案:Missing URI template variable ‘userName‘ for method parameter of type String

简介: 解决方案:Missing URI template variable ‘userName‘ for method parameter of type String

@TOC

解决方案

问题:

@RequestMapping("login")
    public String Login(@PathVariable String userName,
                        @PathVariable String passWord){
    }

运行时出现错误:

WARN 5404 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingPathVariableException: Missing URI template variable 'userName' for method parameter of type String]

问题原因1:

路径中没有对应的参数或者参数名不对,需要定义好参数

解决方案1:

在url路径中定义好参数。

@RequestMapping("login/{userName}/{passWord}")
    public String Login(@PathVariable String userName,
                        @PathVariable String passWord){
    }

问题原因2:

如果使用的是Vue+Springboot前后端分离,想要传递数据,传递的url可能是:http://localhost:8080/test/login?userName=xiaohua&passWord=dwad

那么此时你使用的应当是@RequestParam,而不是@PathVariable

解决方案2:

@RequestMapping("login")
    public String Login(@RequestParam String userName,
                        @RequestParam String passWord){
    }

PathVariable和RequestParam的使用

地址1:http://localhost:8080/test/login?userName=xiaohua&passWord=dwad

地址2:http://localhost:8080/test/login/userName=xiaohua/passWord=dwad

如果想获取地址1中的 pageNo的值 ‘userName’ ,则使用  @RequestParam ,

如果想获取地址2中的 emp/7 中的 ‘userName ’   则使用 @PathVariable

@PathVariable

从路径中获取变量,也就是把路径当做变量。

  使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId},这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

@RequestParam

从请求中获取参数

  常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;

相关文章
|
JavaScript API
required string parameter ‘XXX‘is not present 的几种情况
required string parameter ‘XXX‘is not present 的几种情况
3187 0
|
11月前
|
SQL 数据库
【YashanDB 知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
【YashanDB 知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
|
11月前
|
SQL 数据库
【YashanDB知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
【YashanDB知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
解决Missing cookie ‘JssionId‘ for method parameter of type String问题
解决Missing cookie ‘JssionId‘ for method parameter of type String问题
356 0
|
SQL
Typecho——Argument 1 passed to Typecho\Router::get() must be of the type string
Typecho——Argument 1 passed to Typecho\Router::get() must be of the type string
232 0
|
SQL JavaScript
js开发:请解释什么是ES6的模板字符串(template string),并给出一个示例。
ES6的模板字符串以反引号包围,支持变量和表达式插入以及多行书写。例如,插入变量值`Hello, ${name}!`,计算表达式`${num1 + num2}`,以及创建多行字符串。模板字符串保留原始空格和缩进,简化了字符串拼接,提高了代码可读性。
245 6
Excel上传出错:TypeError [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string or an instan
Excel上传出错:TypeError [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string or an instan
|
人工智能 自然语言处理 语音技术
Invalid prop: type check failed for prop “index“. Expected String with value “5“问题解决
Invalid prop: type check failed for prop “index“. Expected String with value “5“问题解决
466 0
|
XML 数据格式
解决 Cannot convert value of type ‘java.lang.String‘ to required type ‘java.sql.Driver‘ for property ‘
解决 Cannot convert value of type ‘java.lang.String‘ to required type ‘java.sql.Driver‘ for property ‘
699 0