请求映射返回错误 404 [英] Request Mapping returning error 404
问题描述
这是我的控制器,将请求映射到这个 url http://localhost:8080/SpringMVCJSON/rest/kfc/brands
This is my controller that maps a request to this url http://localhost:8080/SpringMVCJSON/rest/kfc/brands
控制器文件
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value = "{name}", method = RequestMethod.GET)
public @ResponseBody
Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[] { "name1", "name2" });
return shop;
}
这是带有 servlet 请求的 web.xml,它与 url 一起发送请求/响应
this is the web.xml with the servlet request that dispatches the request/response along with the url
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
假设一切正常,当我在这个 url 上启动我的应用程序时,它返回错误 404 http://localhost:8080/SpringMVCJSON/rest/kfc/brands
我的服务器控制台返回此警告
Assuming that everything is alright, when I launch my app on this url it returns error 404 http://localhost:8080/SpringMVCJSON/rest/kfc/brands
My server console returns this warning
Apr 26, 2016 12:14:47 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCJSON/rest/kfc/brands] in DispatcherServlet with name 'mvc-dispatcher'
请问tomcat为什么不把请求映射到服务器?
Please why is tomcat not mapping request to the server?
推荐答案
您将控制器配置为在 /kfc/brands/{name}
URL 上可用,但试图在 上访问它/kfc/brands
.
You configured your controller to be available on /kfc/brands/{name}
URL but trying to access it on /kfc/brands
.
您可以在此处找到有关使用 @RequestMapping
的更多信息:http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
Here you can find more information about using @RequestMapping
: http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
这篇关于请求映射返回错误 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!