2

I'm currently working on an existing project (java version: 1.7.0, using Spring MVC framework version: 3.1.4) where we only have located this PathVariable issue in only one URI of an specific controller class, and I would really appreciate if any of you have already faced this error and how did you fix/solve it? Thanks!

Code (extracted part of the java controller class):

@RequestMapping(value = "/site/apps/{question}.json", method = RequestMethod.GET)
public @ResponseBody ServiceResponse moreUsers(
        @PathVariable("question") final Question question,
        @RequestParam(value = "sort", required = false) final String sort,
        final HttpServletRequest request, final Model model)

Error output of our tomcat log (catalina.out file):

ERROR Error executing request: /site/apps/52440.json
org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public com.project.infrastructure.service.ServiceResponse com.project.plugins.controllers.SiteAppController.moreUsers(com.project.models.node.Question,java.lang.String,javax.servlet.http.HttpServletRequest,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: **Could not find @PathVariable [question] in @RequestMapping**

What we've already researched:

Alexander B
  • 1,023
  • 7
  • 20
Hernan A
  • 21
  • 3
  • Is there any url patterns similar to this? And why your parameter type is `Question`? – Blank Jun 21 '16 at 01:55
  • `@PathVariable` should be a `String`, not a `Question`. – fatiherdem Jun 21 '16 at 02:49
  • No, there isn't any url pattern similar to this, there is no duplicated URI on this project. It was originally coded by using a custom Question object that has and id field into it. But, in order to be sure this issue is not related with that, I already tested by replacing Question object by a Long object and also even by using a long primitive as well – Hernan A Jun 21 '16 at 03:08
  • According to spring doc (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates), a @PathVariable argument can be of any simple type such as int, long, Date, etc. Spring automatically converts to the appropriate type or throws a TypeMismatchException if it fails to do so. – Hernan A Jun 21 '16 at 03:18
  • have you added to your dispatcherServlet? @HernanA – Tahir Hussain Mir Jun 21 '16 at 06:36
  • Yes @TahirHussainMir, it has been already added. And if it's related with this, any PathVariable annotation shouldn't work on my java project, and that's not the case. Anyway thks for the reply. – Hernan A Jun 21 '16 at 13:55
  • what is question? @HernanA – Tahir Hussain Mir Jun 22 '16 at 05:13
  • Question is a model object that has several fields, and the important one named as id and type as Long. – Hernan A Jun 22 '16 at 13:38

1 Answers1

0

In your method handler try to use {question} pathVariable as int, assuming you have int questionid field in your Question JavaBean & try to construct Question JavaBean inside method handler..

@RequestMapping(value = "/site/apps/{question}.json", method = RequestMethod.GET)
public @ResponseBody ServiceResponse moreUsers(
        @PathVariable("question") Integer question,
        @RequestParam(value = "sort", required = false) final String sort,
        final HttpServletRequest request, final Model model){

Question Q=questionservice.getQuestion(question);

}
Sagar Kadu
  • 251
  • 2
  • 18
  • As I mentioned above, I haven't tried with Integer, but I already tested by replacing Question object by a Long object, and also even by using a long primitive as well. – Hernan A Jun 21 '16 at 13:57