Spring MVC 3.2.4 @RequestMapping不起作用

时间:2013-09-21 07:37:00

标签: java spring spring-mvc

尝试使用spring mvc创建restful服务,但无法访问它。在阅读了大量答案之后,这些似乎都是正确的,也许你可以看到一些东西。

21.09.2013 10:50:33 org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/rest/book] in DispatcherServlet with name 'mvc-dispatcher'

的web.xml

<web-app 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_3_0.xsd"
         version="3.0" metadata-complete="true">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                ru.expbrain.flib.config.RestConfig
            </param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.hmtl</welcome-file>
    </welcome-file-list>

</web-app>

RestConfig.java

package ru.expbrain.flib.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"ru.expbrain.flib.rest.controller"})
public class RestConfig  extends WebMvcConfigurerAdapter {

}

BookController.java

package ru.expbrain.flib.rest.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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ru.expbrain.flib.rest.entity.Book;

import java.util.concurrent.atomic.AtomicLong;

@Controller
public class BookController {

    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(value="/book", method = RequestMethod.GET)
    public @ResponseBody Book greeting(@RequestParam(value="content", required=false, defaultValue="World") String name) {
        return new Book(counter.incrementAndGet(), name);
    }
}

尝试通过路径获取内容,顺便说一下根上下文路径转到/

http://localhost:9080/rest/book

1 个答案:

答案 0 :(得分:0)

感谢Sotirios Delimanolis的测试。

这是缓存的一些问题...无法解释发生了什么,因为它不是用清洁修复......但重新创建项目帮助,一切似乎都很好。