2013-02-21 75 views
0

我一直在爲這個消息奮戰了幾天,無法弄清楚我有什麼問題。在DIspatcherServlet中找不到映射

基本上我想要做的是用我的服務提供json服務。我不想返回一個jsp。

我請求的URL是

localhost/service/products/1 

以下是錯誤。

WARN - No mapping found for HTTP request with URI [/service/products/1] in DispatcherServlet with name 'cr' 

我的ProductsController顯示如下:

package com.cr.controllers; 

import com.cr.dao.ProductsDao; 
import com.cr.entity.Products; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import javax.servlet.ServletResponse; 
import java.io.IOException; 

/** 
* User: ChappleZ 
* Date: 2/17/13 
* Time: 8:21 PM 
*/ 
@Controller 
@RequestMapping(value = "/service") 
public class ProductsController { 
    private static final Logger log = LoggerFactory.getLogger(ProductsController.class); 
    @Autowired 
    private ProductsDao productsDao; 

    @RequestMapping(method = RequestMethod.GET) 
    public void get(ServletResponse response) throws IOException { 
     response.setContentType("text/plain"); 
     response.getWriter().print("My Products dao: " + productsDao); 
    } 

    @RequestMapping(value = "/products/{productId}", 
      headers="Accept=application/json", 
      method = RequestMethod.GET) 
    public 
    @ResponseBody 
    Products findByProductId(@PathVariable Long productId) { 
     Products products = productsDao.getProductsById(productId); 
     return products; 
    } 
} 

應用程序上下文:

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     "> 
    <context:annotation-config /> 
    <jpa:repositories base-package="com.cr" /> 

    <!-- // JPA specific configuration here: dataSource, persistenceUnitManager exceptionTranslator, entityManagerFactory, SessionFactory, transactionManager - should not be relevant for this problem, tell me if i'm wrong--> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <bean id="productsDao" class="com.cr.dao.impl.ProductsDaoImpl"/> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="persistenceUnitName" value="spring-jpa"/> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true"/> 
       <property name="generateDdl" value="true"/> 
       <property name="database" value="MYSQL"/> 
      </bean> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <tx:annotation-driven/> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/crcart3"/> 
     <property name="username" value="root"/> 
     <property name="password" value=""/> 
    </bean> 
</beans> 

的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"> 
    <display-name>cr</display-name> 
    <description>cr</description> 
    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>classpath:log4j.xml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      classpath:applicationContext.xml 
     </param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 


    <servlet> 
     <servlet-name>cr</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/conf/spring-controllers.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>cr</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <filter> 
     <filter-name>httpMethodFilter</filter-name> 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>httpMethodFilter</filter-name> 
     <servlet-name>cr</servlet-name> 
    </filter-mapping> 

    <session-config> 
     <session-timeout>15</session-timeout> 
    </session-config> 

    <error-page> 
     <error-code>401</error-code> 
     <location>/error/401</location> 
    </error-page> 
    <error-page> 
     <error-code>404</error-code> 
     <location>/error/404</location> 
    </error-page> 
    <error-page> 
     <error-code>500</error-code> 
     <location>/error/500</location> 
    </error-page> 
    <error-page> 
     <error-code>504</error-code> 
     <location>/error/504</location> 
    </error-page> 
    <error-page> 
     <exception-type>java.lang.Throwable</exception-type> 
     <location>/error/500</location> 
    </error-page> 

</web-app> 

彈簧Controllers.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:util="http://www.springframework.org/schema/util" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:oxm="http://www.springframework.org/schema/oxm" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> 

    <context:property-placeholder location="classpath:config.properties"/> 

    <context:component-scan base-package="com.cr"/> 

    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
     <property name="supportedMediaTypes" value="application/json"/> 
    </bean> 

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <list> 
       <ref bean="jsonConverter"/> 
      </list> 
     </property> 
    </bean> 

    <mvc:resources mapping="/resources/**" location="/resources/"/> 

</beans> 

此外,如果它有幫助,這也顯示與404頁面相同的警告。

WARN - No mapping found for HTTP request with URI [/service/products/1] in DispatcherServlet with name 'cr' 
WARN - No mapping found for HTTP request with URI [/error/404] in DispatcherServlet with name 'cr' 
+1

你應該表現出請求被髮送。 – 2013-02-21 02:00:08

+0

我更新了我正在使用的網址。順便說一句,@RyanStewart你的github測試平臺真棒。這真的有助於讓我走得這麼遠。我掙扎了兩個星期,直到我發現這一點,並且讓我走得這麼遠。 – zmanc 2013-02-21 02:36:22

+0

不只是網址,整個請求。 – 2013-02-21 02:37:45

回答

1

三個問題,我看到:

  1. 在web.xml中,你的DispatcherServlet應該被映射到/*,而不是僅僅/。後者只匹配空路徑,沒有別的,所以你請求的路徑永遠不會命中servlet。
  2. 你離開了你的spring-controller.xml中的<mvc:annotation-driven/>
  3. 在您的spring-controllers.xml中可能不相關但肯定會導致將來出現問題,您不應該像在applicationContext.xml中那樣對組件進行組件掃描。 spring-controllers.xml用於配置DispatcherServlet,並且應該只有控制器和與MVC相關的bean。 applicationContext.xml是你的服務,DAO和相關事物應該存在的地方。這是因爲這裏所描述的同樣的問題:

Declaring Spring Bean in Parent Context vs Child Context

的配置Spring MVC中的Spring上下文的適當方式進一步解釋,可以發現:

Why DispatcherServlet creates another application context?

相關問題