解决方案: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的值;

相关文章
|
2月前
|
前端开发 Java 数据库连接
Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
|
2月前
|
SQL JavaScript
js开发:请解释什么是ES6的模板字符串(template string),并给出一个示例。
ES6的模板字符串以反引号包围,支持变量和表达式插入以及多行书写。例如,插入变量值`Hello, ${name}!`,计算表达式`${num1 + num2}`,以及创建多行字符串。模板字符串保留原始空格和缩进,简化了字符串拼接,提高了代码可读性。
28 6
|
9月前
解决Missing cookie ‘JssionId‘ for method parameter of type String问题
解决Missing cookie ‘JssionId‘ for method parameter of type String问题
110 0
|
8月前
|
编译器
error TS2322 Type ‘string null‘ is not assignable to type ‘string undefined‘.
error TS2322 Type ‘string null‘ is not assignable to type ‘string undefined‘.
|
2月前
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
SAP ABAP 字符串模版(String Template)核心知识点举例说明试读版
SAP ABAP 字符串模版(String Template)核心知识点举例说明试读版
|
9月前
|
人工智能 自然语言处理 语音技术
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“问题解决
72 0
|
9月前
|
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 ‘
173 0
|
Java
【报错】String cannot be resolved to a type解决方案
【报错】String cannot be resolved to a type解决方案
287 1
|
11月前
|
JSON 数据格式
Required request parameter ‘name‘ for method parameter type String is not present 报错解决方法
Required request parameter ‘name‘ for method parameter type String is not present 报错解决方法
3484 0