Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

passing id value to controller gives an error " Failed to convert the value of type 'java.lang.String' to required type 'int'".

but when I print System.out.println(studentService.findById(id)) it give a output in terminal as Optional[Student{id=1, firstName='Abhishek', lastName='Shukla', branch='CS', year=4, mobileNumber='9876543210'}]

I already tried to pass the id as a string and convert string to int by using Integr.parseInt(id) in the controller but it throws an error as " Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException: For input string: "{id}"] with root cause "

<h1>Student Place</h1> <form name="myForm" action="/student/{id}" > <table border="1"> <tbody> <td>Student ID :</td> <td><input type="text" name="id" size="10" /> </td> </tbody> </table> <input type="submit" value="submit" name="submit" /><br> <input type="reset" value="Rest" name="reset" /> </form> @GetMapping(value = "/students") public String index(ModelMap modelMap){ modelMap.put("students" , studentService.findAll()); return "index" ; @GetMapping(value = "/student/{id}") public String data(@PathVariable("id") int id , ModelMap modelMap){ modelMap.put("students" , studentService.findById(id)) ; System.out.println(studentService.findById(id)); return "studentbyid" ; @GetMapping(value = "/student") public String databyid(){ return "student" ; @Transactional @Service("studentService") public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository ; @Override public Iterable<Student> findAll() { return studentRepository.findAll(); public Optional<Student> findById(int id) { return studentRepository.findById(id); public Student(int id, String firstName, String lastName, String branch, int year, String mobileNumber) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.branch = branch; this.year = year; this.mobileNumber = mobileNumber; public int getId() { return id; public void setId(int id) { this.id = id; public String getFirstName() { return firstName; public void setFirstName(String firstName) { this.firstName = firstName; public String getLastName() { return lastName; public void setLastName(String lastName) { this.lastName = lastName; public String getBranch() { return branch; public void setBranch(String branch) { this.branch = branch; public int getYear() { return year; public void setYear(int year) { this.year = year; public String getMobileNumber() { return mobileNumber; public void setMobileNumber(String mobileNumber) { this.mobileNumber = mobileNumber; @Override public String toString() { return "Student{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", branch='" + branch + '\'' + ", year=" + year + ", mobileNumber='" + mobileNumber + '\'' +

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "{id}"

So, as the error message shows, you are literally sending a request to /student/{id} instead of, let's say, /student/1234 .

Since apparently, the form has an input field where the user must enter the ID, the ID will be sent as a request parameter, not as a path variable, so the path of your controller method and of your action should simply be

/students

and it should take the ID as a request parameter:

@GetMapping(value = "/student")
public String data(@RequestParam("id") int id, ModelMap modelMap) 

Try using @RequestParam instead of @PathVariable as you're already sending <input name = "id">, so

Like, that's a parameter you're already sending, so you should fetch it as a param, not as Path Variable.

Once you do so, keep the url as /students instead of /students/{id}

Should work then.

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Jan 9 at 15:26

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.