1

This is my controller:

@Controller
@RequestMapping("/area")
public class AreaController {

    @RequestMapping("/{id}")
    public String getPage(@PathVariable("id") int id){
        return "asd3333";
    }

}

and this is what I get when I access http://localhost:8080/area/1:

  • Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "asd3333"]

I tested this random return just to show what is happening... The method is beeing called first with the @PathVariable = 1 from the request, and then right after that, is called again with the whethever the method resulsts, in this case, it tries to pass the @PathVariable = "asd3333".

I have NO IDEA of what tha heck is happening, pls help

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

3

Sounds very strange indeed. I will start with a question

@RequestMapping("/{id}")
public String getPage(@PathVariable("id") int id){
    return "asd3333";
}

Does this method need to be called for all method types (Get, Post, Delete, ...). If no try to restrict with a specific method call.

ex

 @RequestMapping(value = "/{id}", method = POST)

GOTCHA.

Also add this to the method because you return a simple string

@RequestMapping("/{id}")
@ResponseBody
public String getPage(@PathVariable("id") int id)

Also if you don't plan to use this API as a web MVC application but instead as a rest API backend switch from @Controller to @RestController.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • 1
    As of a few years ago, `@GetMapping` is usually preferred to `method = GET`. – chrylis -cautiouslyoptimistic- Dec 19 '20 at 22:10
  • Yes, '@ResponseBody' solved it. Initially I was trying to render a page with thymeleaf, so that's why I was using '@Controller'. And I forgot to put the thymeleaf dependencie, and that's why it wasn't working with just a ModelAndView return and no '@ResponseBody'. Thanks – Murillo Tavares Dec 19 '20 at 22:52