Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing URI template variable 'studentId' for method parameter type [java.lang.Integer] - Spring MVC

I'm getting this error when I try to redirect to a certain view.

In one handler method i have:

// get student ID, add it to model, and return redirect URI
Integer studentId = student.getStudentId();
model.addAttribute("studentId", studentId);
return "redirect:/students/{studentId}";

But I'm not getting the parameter studentId in this handler method:

@RequestMapping(value="/{student}", method = RequestMethod.GET)
public String getStudent(@PathVariable Integer studentId, Model model) {

    Student student = studentService.get(studentId);
    model.addAttribute("student", student);

    return "student";
}

What am I missing here?

like image 531
just_a_girl Avatar asked Dec 20 '13 23:12

just_a_girl


1 Answers

If you don't specify the name of the path variable, Spring tries to use the name of your parameter.

Therefore in

@RequestMapping(value="/{student}", method = RequestMethod.GET)
public String getStudent(@PathVariable Integer studentId, Model model) {

Spring will try to find a path variable called studentId while you have a path variable called student.

Just add a value attribute

@PathVariable("student") Integer studentId

or change the parameter name.

like image 179
Sotirios Delimanolis Avatar answered Nov 15 '22 23:11

Sotirios Delimanolis