FIXED: "No match found." Trying to set up a RESTfull interface using Spring -MVC

This issue has been resolved. Someone created a file called mvc-dispatcher.xml and had the wrong configuration. This file is loaded automatically because it is called the same as the servlet.

I want to thank everyone who tried to help me fix this problem. I ask this question here, as it actually explains how to create a REST interface. It works great.


I am trying to configure a RESTful interface using Spring-MVC. The server starts without problems, but at any time when I try to call the REST interface, I get a message:

41 WARN [springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/myweb/rest/asd/aaa] in DispatcherServlet with name 'mvc-dispatcher' 

It seems that the URL I'm sending ( http://localhost:8080/myweb/rest/asd/qwe for example) is not "captured" by any controller. What am I doing wrong?

I do not know what else I could try. I am using Java 1.7.0_15, Tomcat 7.0.34 and Spring 3.1.4.RELEASE

In my web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>MyWeb</display-name> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> <!-- Listener for MVC spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- Servlet for MVC spring --> <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> <!-- Loading web properties --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> 

In my application, context.xml:

 <context:component-scan base-package="com.myweb.*" /> <context:annotation-config/> <tx:annotation-driven/> <mvc:annotation-driven /> 

And finally, my controller class:

 import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RestController { private static Logger LOG = Logger.getLogger(RestController.class); @RequestMapping("/asd/{test}") public void test(@PathVariable String test) { LOG.info("You sent "+test); } } 

I tried changing the @RequestMapping method, but still did not work:

 @RequestMapping(method=RequestMethod.GET) public void test() { LOG.info("You sent something"); } 
+2
source share
2 answers

Your mapping is incorrect.

You request /rest/asd/aaa , but your mapping is done for /asd/{test} .

You need to change @RequestMapping("/asd/{test}") to @RequestMapping("/rest/asd/{test}")

Or add @RequestMapping("/rest") to your controller class

+3
source

Try annotating your controller class and @RequestMapping("/rest") to handle all requests with a url

/ Webapp / leisure /

You should also set your context through the org.springframework.web.context.ContextLoaderListener listener. I think you are not loading applicationContext.xml

Add this part to your web.xml

 <!--spring bootstrap listener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 
0
source

Source: https://habr.com/ru/post/1236677/


All Articles