【发布时间】:2016-07-27 17:11:11
【问题描述】:
已根据此答案解决 Spring invokes wrong controller mapping Spring @Controllers URL 总是相对于处理它们的 Spring Dispatcher Servlet 进行解释。因此,如果您将调度程序 servlet 映射到 web.xml 中的 /api/,那么上面控制器的 URL 就是 /api/api/choice
双字符串 service/service/1234 正在工作。
原帖
访问 REST 资源端点给我一个 404 错误,尽管一切似乎都定义正确:
日志输出:
DEBUG DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/myapp/service/1234]
DEBUG Looking up handler method for path /1234
DEBUG Did not find handler method for [/1234]
WARN No mapping found for HTTP request with URI [/myapp/service/1234] in DispatcherServlet with name 'mvc-dispatcher'
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
SpringMVC 控制器
@RestController
@RequestMapping("/service")
public class RESTController {
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public String getResult ( @PathVariable String id )
{
//... get JSON result
}
}
预期调用:myapp/service/1234
还尝试了以下选项: 1)不要定义一个类RequestMapping,只做一个Method Request Mapping
@RequestMapping("/service/{id}")
2) 以及
@RequestMapping("/service*/{id}")
@RequestMapping("/service**/{id}")
通过上面的日志继续收到 404。
【问题讨论】:
标签: spring rest spring-mvc web.xml