Issue
I want to pass two variables in the url to my Spring Controller.
I'm trying to achieve this using the following code. The controller though reads just the second param.
@RestController
@RequestMapping("/service/getVars")
public class SpringServiceController {
@RequestMapping(value = "/Id/{Id}/Name/{Name}", method = RequestMethod.GET)
public String getGreeting(@PathVariable String Id, @PathVariable String Name) {
//Both id and name now holds Name variables value.
System.out.println("Id: "+ Id + " >> Name: " + Name);
}
}
i/p: localhost:8080/service/getVars/Id/111/Name/222
o/p: Id: 222 >> Name: 222
Expected o/p: Id: 111 >> Name: 222
Solution
This might depend on the way you are compiling your source code. If the parameter names are not included in the byte code, I don't think the behavior of @PathVariable
without a value
attribute is defined. Add it explicitly
@RequestMapping(value = "/Id/{Id}/Name/{Name}", method = RequestMethod.GET)
public String getGreeting(@PathVariable(value = "Id") String Id, @PathVariable(value = "Name") String Name) {
Answered By - Sotirios Delimanolis
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)