问题
This is my controller that maps a request to this url http://localhost:8080/SpringMVCJSON/rest/kfc/brands
contoller file
@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;
}
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>
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'
Please why is tomcat not mapping request to the server?
回答1:
You configured your controller to be available on /kfc/brands/{name}
URL but trying to access it on /kfc/brands
.
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
来源:https://stackoverflow.com/questions/36863440/request-mapping-returning-error-404