2013-09-21 79 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進行測試。

這是高速緩存的一些問題......無法解釋發生了什麼,因爲它不是用清潔修復......但重新創建項目幫助和一切似乎都很好。