请求映射返回错误404

时间:2016-04-26 11:20:44

标签: spring spring-mvc tomcat

这是我的控制器将请求映射到此网址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

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

假设一切正常,当我在此网址上启动我的应用时,它会返回错误404 http://localhost:8080/SpringMVCJSON/rest/kfc/brands 我的服务器控制台返回此警告

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没有将请求映射到服务器?

1 个答案:

答案 0 :(得分:0)

您已将控制台配置为在/kfc/brands/{name}网址上可用,但尝试在/kfc/brands上访问该控制器。

您可以在此处找到有关使用@RequestMappinghttp://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

的更多信息