The spring boot exception Missing URI template variable for method parameter of type String occurs when the path variable name in the @RequestMapping annotation is different from the @PathVariable name. The name of the path variable should be the same in @RequestMapping and @PathVariable. If there is some mismatch, the exception Missing URI template variable for method parameter of type String would be thrown.
The path variable is added as part of the request url in the @RequestMapping annotation. The path variable is covered by curly brackets. The path variable is added to the method parameter with a @PathVariable annotation. The name of the method parameter and the name of the path variable should be the same to avoid an exception Missing URI template variable ” for method parameter of type String.
Exception
The exception Missing URI template variable for method parameter of type String will be shown in the browser as below.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Dec 30 21:31:19 IST 2020
There was an unexpected error (type=Internal Server Error, status=500).
Missing URI template variable 'name' for method parameter of type String
The spring boot console log will be the exception as seen below.
2020-12-30 21:31:19.843 WARN 43928 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingPathVariableException: Missing URI template variable 'name' for method parameter of type String]
$ curl -X GET http://localhost:8080/welcome/test
{"timestamp":"2020-12-30T16:13:52.084+0000","status":500,"error":"Internal Server Error","message":"Missing URI template variable 'name' for method parameter of type String","path":"/welcome/test"}
Root Cause
The exception Missing URI template variable ” for method parameter of type String is triggered by the inconsistency of the path variable in the request url in the @RequestMapping annotation and the method variable name in the @PathVariable annotation. The path variable name between the @RequestMapping annotation and the @PathVariable annotation should be the same.
How to Reproduce this exception
If the path variable name is created differently in both @RequestMapping annotation and @PathVariable annotation, the exception would be thrown due to the name mismatch.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome/{username}", method=RequestMethod.GET)
public String welcomepage(@PathVariable String name) {
return "Welcome to "+name;
}
}
Output
$ curl -X GET http://localhost:8080/welcome/test
{"timestamp":"2020-12-30T16:20:36.906+0000","status":500,"error":"Internal Server Error","message":"Missing URI template variable 'name' for method parameter of type String","path":"/welcome/test"}
Solution 1
If the path variable name in the @RequestMapping annotation is different from the @PathVariable annotation, change the path variable name in the @RequestMapping annotation. The name in the @ReqeustMapping annotation should be the same as the name in the @PathVariable annotation.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome/{name}", method=RequestMethod.GET)
public String welcomepage(@PathVariable String name) {
return "Welcome to "+name;
}
}
Output
$ curl -X GET http://localhost:8080/welcome/yawin
Welcome to yawin
Solution 2
If the method variable name in the annotation @PathVariable is different from the annotation @RequestMapping, change the path variable name in the annotation @PathVariable. The name in the annotation @PathVariable should be same as the name in the annotation @RequestMapping.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome/{username}", method=RequestMethod.GET)
public String welcomepage(@PathVariable String username) {
return "Welcome to "+username;
}
}
Output
$ curl -X GET http://localhost:8080/welcome/yawin
Welcome to yawin
Solution 3
If the path variable name in the @PathVariable annotation and the method variable name in the @RequestMapping annotation are different and could not modify either name, the path variable name can be modified as the annotation attribute in the @PathVariable. The name in the @PathVariable annotation should be the same as the name in the @RequestMapping annotation, and the method variable name might be different.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome/{username}", method=RequestMethod.GET)
public String welcomepage(@PathVariable("username") String name) {
return "Welcome to "+name;
}
}
Output
$ curl -X GET http://localhost:8080/welcome/yawin
Welcome to yawin