java.lang.IllegalStateException: 找不到@PathVariable

3
我目前正在处理一个现有项目(Java版本:1.7.0,使用Spring MVC框架版本:3.1.4),我们只在特定控制器类的一个URI中发现了PathVariable问题。如果您已经遇到过这个错误并且知道如何修复/解决它,我将不胜感激。谢谢! 代码(Java控制器类的提取部分):
@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)

错误输出我们的Tomcat日志(catalina.out文件):

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**

我们已经研究过的内容:


有没有类似于这样的URL模式?为什么你的参数类型是Question - undefined
@PathVariable应该是一个String,而不是一个Question - undefined
没有,没有类似于这样的URL模式,在这个项目中没有重复的URI。最初是使用一个自定义的Question对象编码的,该对象具有一个id字段。但为了确保这个问题与此无关,我已经通过将Question对象替换为Long对象进行了测试,甚至还使用了长整型原始数据类型。 - undefined
根据Spring文档(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates),@PathVariable参数可以是任何简单类型,如int、long、Date等。Spring会自动转换为适当的类型,如果无法转换,则会抛出TypeMismatchException异常。 - undefined
你是否在你的dispatcherServlet中添加了mvc:annotation-driven/?@HernanA - undefined
显示剩余3条评论
1个回答

0
在你的方法处理程序中,尝试将 {question} pathVariable 作为 int 使用,假设你的 Question JavaBean 中有一个 int questionid 字段,并尝试在方法处理程序内构造 Question JavaBean。
@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);

}

正如我上面提到的,我还没有尝试过使用Integer,但我已经通过将Question对象替换为Long对象进行了测试,甚至还使用了long原始类型。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接