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

解决方案

问题:

@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的值;

猜你喜欢

转载自blog.csdn.net/air__Heaven/article/details/123618649