Issue
I'm struggling with attaining an id from the url to use as a parameter in my read() method. I read and have seen dusin of examples of using @PathVariable and I can't see why this should not work.
This is my controller class.
@GetMapping("details/{id}")
public String read(@PathVariable int employeeId, Model model)
{
model.addAttribute("students_data", studentsRepo.read(employeeId));
//the line underneath will work using as an example the int 2 in the parameter. But I want the int coming from the url.
//model.addAttribute("students_data", studentsRepo.read(2));
return "details";
}
I get the error on the details page:
Fri Jan 03 12:13:44 CET 2020
There was an unexpected error (type=Not Found, status=404).
No message available
Example of how the url could look is:
http://localhost:8080/details?id=2
Solution
The URL http://localhost:8080/details?id=2
that you have shared contain @RequestParam and not @PathVariable
If you want to use @RequestParam then your API signature should be
@GetMapping("details/")
public String read(@RequestParam("id") int employeeId, Model model)
{
"details";
}
If you want to use @PathVariable then your API should be
@GetMapping("details/{id}")
public String read(@PathVariable("id") int employeeId, Model model)
{
"details";
}
Please check the difference between the two https://javarevisited.blogspot.com/2017/10/differences-between-requestparam-and-pathvariable-annotations-spring-mvc.html
Answered By - dassum