3

This issue is solved. Someone created a file called mvc-dispatcher.xml and it had an incorrect configuration. That file is loaded automatically because it's called the same as a servlet.

I want to thank everyone who tried to help me fix this issue. I'm keeping this question here, since it actually explains how to create a REST interface. It works perfectly.


I'm trying to set up a RESTful interface with Spring-MVC. The server starts without issue, but any time I try to call the REST interface I get the 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 am 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'm 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 applicationContext.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);
    }
}

Tried changing the method's @RequestMapping but still didn't work:

@RequestMapping(method=RequestMethod.GET)
public void test() {
    LOG.info("You sent something");
}
Calabacin
  • 725
  • 2
  • 8
  • 19

2 Answers2

3

Your mapping is wrong.

You are requesting /rest/asd/aaa but your mapping is for /asd/{test}.

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

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

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Thanks a lot for your reply! Tried to add `@RequestMapping("/rest")` to the class, but got the same result. Also, changing the method's RequestMapping to `@RequestMapping("/rest/asd/{test}")` (removing the class' RequestMapping) didn't work either. – Calabacin Mar 01 '13 at 17:20
  • are you sure whether the controller is loaded? – Arun P Johny Mar 01 '13 at 17:26
  • I have this line in my applicationContext.xml: ``. The controller class is in com.myweb.media.controller – Calabacin Mar 01 '13 at 17:55
0

Try annotate your controller class also with @RequestMapping("/rest") to handle all requests with url

/webapp/rest/

You should also configure your context via 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>
Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
  • Thanks a lot for your reply. Tried that, and also tried to change the method's request to `@RequestMapping("/rest/asd/{test}")` instead, but neither would work. – Calabacin Mar 01 '13 at 17:25
  • Hmm are You scanning for the controller to be loaded? – Arsen Alexanyan Mar 01 '13 at 17:27
  • I have this line in my applicationContext.xml: ``. The controller class is in com.myweb.media.controller – Calabacin Mar 01 '13 at 17:42
  • @Calabacin Have You added the new part in your web.xml posted by me? – Arsen Alexanyan Mar 01 '13 at 17:43
  • I apologize I didn't include that part of my web.xml, I tried to keep it simple by not including the JSF parts, and it turns out I removed more than intended. I updated the web.xml in my original post – Calabacin Mar 01 '13 at 17:49
  • @Calabacin I'm not sure but try also – Arsen Alexanyan Mar 01 '13 at 17:58
  • That didn't work either, although it was a good test. As soon as I did that, most of my other classes lost their small (S) meaning that Spring no longer saw those annotations. I also tried adding several different methods, each with a different @RequestMapping, but none was called. – Calabacin Mar 02 '13 at 12:03