2013-09-21 80 views
0

尝试用spring mvc创建宁静的服务,但无法访问它。在阅读了大量的答案之后,看起来都是正确的,也许你会看到一些东西。Spring MVC 3.2.4 @RequestMapping不工作

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 
+0

检查应用程序启动时的日志,您应该看到调度器映射的详细信息 – 2013-09-21 08:05:05

+0

您确定您的'BookController'正在实例化吗? (您可以使用'@ PostConstruct'方法编写日志消息。) – chrylis

+0

没有提到任何映射过程,但它显示mvc-dispatcher-servlet初始化 – ArchCC

回答

0

谢谢Sotirios Delimanolis进行测试。

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