Spring MVC Rest: Mapping mappings for HTTP request with URI [/ ecommerce-api / rest / checkout] in DispatcherServlet

(possibly) resolved `or at least it looks like. I am not quite sure where this is the problem. Surely the configuration proposed by Biju Kunjummen works and seems cleaner to me. What I am doing now so as not to create a mess is to work only inside Eclipse, sometimes cleaning projects and never using maven to package and deploy it (at least in everyday programming, I think with some reliable maven script or CI Server everything will work fine).

I am trying to configure the Rest API using Spring MVC. I read a lot of documentation, but I still get the error in the topic:

No mapping for HTTP request with URI [/ ecommerce-api / rest / checkout] in DispatcherServlet

The problem is exactly the same as reported (and resolved) in other similar issues, like this FIXED: "No mapping found" Attempt to configure the RESTfull interface using Spring -MVC

It is really strange that without changing anything in my code, once this works, and sometimes not. I am almost sure that nothing changes between these two points, because, for example, once I’m physically away, then I return and stop working.

At this specific moment, my code is as follows:

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/api-dispatcher-servlet.xml </param-value> </context-param> <servlet> <servlet-name>api-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>api-dispatcher</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> </web-app> 

api-dispatching-servlet.xml

 <?xml version="1.0" encoding="UTF-8" ?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.myapp.api.ecommerce.controller" /> <context:annotation-config /> <mvc:annotation-driven /> </beans:beans> 

com.myapp.ecommerce.controller.CheckoutController

 package com.myapp.api.ecommerce.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.myapp.ecommerce.service.checkout.manager.CheckoutManager; import com.myapp.ecommerce.service.checkout.manager.CheckoutManagerImpl; import com.myapp.ecommerce.service.checkout.model.Checkout; @Controller @RequestMapping("/checkout") public class CheckoutController { private CheckoutManager checkoutManager = new CheckoutManagerImpl(); @RequestMapping(method = RequestMethod.GET) public @ResponseBody Checkout getCheckout() { return checkoutManager.findById("514f2a8e20f7a78a1400001f"); } } 

A fragment of the application server log file (VFabric, but I also tried with Tomcat 7) when I try to GET ecommerce-api / rest / checkout:

 2013-09-05 22:31:37,760 DEBUG [tomcat-http--5] servlet.DispatcherServlet (DispatcherServlet.java:823) - DispatcherServlet with name 'api-dispatcher' processing GET request for [/ecommerce-api/rest/checkout] 2013-09-05 22:31:37,763 DEBUG [tomcat-http--5] handler.AbstractHandlerMethodMapping (AbstractHandlerMethodMapping.java:220) - Looking up handler method for path /checkout 2013-09-05 22:31:37,763 DEBUG [tomcat-http--5] handler.AbstractHandlerMethodMapping (AbstractHandlerMethodMapping.java:230) - Did not find handler method for [/checkout] 2013-09-05 22:31:37,764 WARN [tomcat-http--5] servlet.DispatcherServlet (DispatcherServlet.java:1108) - No mapping found for HTTP request with URI [/ecommerce-api/rest/checkout] in DispatcherServlet with name 'api-dispatcher' 2013-09-05 22:31:37,764 DEBUG [tomcat-http--5] servlet.FrameworkServlet (FrameworkServlet.java:966) - Successfully completed request 

I really don’t know what to do, since it worked the last time I closed my Mac, and until that moment it seemed to me that I knew how to do it.

+4
source share
4 answers

I see one problem, you are loading the same context file ( api-dispatcher-servlet.xml ) twice. Essentially, a typical w760-based web application has two application contexts: the first is the ROOT application context loaded through the ContextLoaderListener, the second is the web application context downloaded via the DispatcherServlet , in your case they both point to exactly the same configuration file that api-dispatcher-servlet.xml . I would recommend you do this and see if this fixes inconsistent behavior:

0.1. Create say a applicationContext.xml file, at the moment it should not contain any beans, later you can put your beans-related service into this file.

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> 

0.2. Download this context through your ContextLoaderListener, into your web.xml:

 <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 

0.3. Download api-dispatcher-servlet.xml through your DispatcherServlet, you have the correct entries for this already, but it may be better to do this explicitly:

 <servlet> <servlet-name>api-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/api-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>api-dispatcher</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> 
+2
source

Strange behavior is often caused by inconsistencies in your compiled and source files. I suggest you do the cleanup correctly (suppose you are using eclipse). Stop the server, then do Project β†’ clean. After that, a clean server. Try restarting the application.

Another tip is to debug what happens when spring searches for the right handler.

+1
source

I use @RestController instead of @Controller and I get the same error.

WARN org.springframework.web.servlet.PageNotFound noHandlerFound - mapping associations for an HTTP request with a URI [/ something / JsonResponseBody ] in a DispatcherServlet named "dispatcher"

Almost everywhere I searched for his expression that we do not need to use @ResponseBody when we use @RestController , however this is exactly what makes it work.

The following works for me, and it will not work if I delete @ResponseBody -

 @RestController public class MyController { @RequestMapping(path = "/dummy") @ResponseBody public DummyObject getDummy() { return new DummyObject("Some data"); } } 

I use Spring MVC 4.x, WebInitiliazer and a 100% code based approach .

Ymmv

+1
source

This happened to me if your web application is working fine and nothing has changed, and it has stopped working, and the steps below are for you:

Hi

0
source

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


All Articles