ไม่พบการแมปคำขอ Spring MVC ไม่พบหน้า 404

ฉันกำลังพัฒนาแอปพลิเคชัน Spring MVC ตัวอย่าง ฉันต้องการส่งคืนสตริง JSON โดยขอตัวควบคุม spring mvc แต่เมื่อฉันส่งคำขอผ่าน url มันแสดงข้อผิดพลาด 404 ฉันกำลังติดตามบทช่วยสอนจากเว็บไซต์ที่มีชื่อเสียงมาก URL ของฉันที่ฉันทำคำขอ localhost:8080/SpringAngularProject/rest/kfc/brands/kfc1 นี่คือรหัสของฉันด้านล่าง:

0-- Console Picture : 
Aug 12, 2014 7:31:19 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringAngularProject/rest/kfc/brands/sdsd] in DispatcherServlet with name 'mvc-dispatcher'

1- มันเป็นคลาสคอนโทรลเลอร์ของฉัน

package com.controller;

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;

@Controller
@RequestMapping("/kfc/brands")
public class HelloWorldController {

    @RequestMapping(value = "{name}", method = RequestMethod.GET)
    public @ResponseBody Shop getShopInJSON(@PathVariable String name) {

        Shop shop = new Shop();
        shop.setName(name);
        shop.setStaffName(new String[] { "staff1", "staff2" });
        return shop;

    }
}

2-- ไฟล์ 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>AngularSpring</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <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>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
  </listener>
</web-app>

3-- ไฟล์ My Spring cfg:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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">
    <context:component-scan base-package="com.controller.HelloWorldController" />
        <mvc:annotation-driven /> 

</beans>

4-- ไฟล์ pom.xml ของฉันสำหรับ maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SpringAngularProject</groupId>
    <artifactId>SpringAngularProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <spring.version>3.2.2.RELEASE</spring.version>
        <jackson.version>1.9.10</jackson.version>
        <jdk.version>1.7</jdk.version>
    </properties>

    <dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.12</version>
        </dependency>




    </dependencies>
</project>

person Junaid Akhtar    schedule 12.08.2014    source แหล่งที่มา
comment
@RequestMapping(value = "/kfc/brands/{name}" ตามเจตนารมณ์ของวิธีการของคุณ   -  person AlexGreg    schedule 12.08.2014
comment
@AlexGreg ไม่ทำงานเช่นกัน   -  person Junaid Akhtar    schedule 12.08.2014
comment
ลองเปลี่ยน <url-pattern>/rest</url-pattern> เป็น <url-pattern>/rest/*</url-pattern> แล้วแจ้งให้เราทราบ   -  person geoand    schedule 12.08.2014
comment
ไม่มีปัญหาเดียวกัน :( @geoand   -  person Junaid Akhtar    schedule 12.08.2014
comment
คุณได้ปรับใช้แอปพลิเคชันของคุณภายใต้บริบท SpringAngularProject หรือไม่   -  person geoand    schedule 12.08.2014
comment
ใช่แล้ว ชื่อโปรเจ็กต์ของฉันคือ SpringAngularProject @geoand   -  person Junaid Akhtar    schedule 12.08.2014
comment
@geoand หลังจากเปลี่ยนเป็นคอนโซลส่วนที่เหลือ/* ให้ข้อผิดพลาดนี้กับฉัน: คำเตือน: ไม่พบการแมปสำหรับคำขอ HTTP ด้วย URI [/SpringAngularProject/rest/kfc/brands/sdsd] ใน DispatcherServlet ที่มีชื่อ 'mvc-dispatcher'   -  person Junaid Akhtar    schedule 12.08.2014
comment
คุณสามารถอัปเดตคำถามของคุณด้วยผลลัพธ์ของบันทึกในโหมด debug เมื่อมีการร้องขอได้หรือไม่   -  person geoand    schedule 12.08.2014
comment
คุณช่วยลอง localhost:8080/rest/kfc/brands/sdsd หน่อยได้ไหม? ขอบคุณ   -  person geoand    schedule 12.08.2014
comment
ยังไม่ได้ผล @geoand   -  person Junaid Akhtar    schedule 12.08.2014
comment
คุณได้ลองใช้ URL ที่ฉันแนะนำพร้อมกับการแมป /rest/* หรือไม่   -  person geoand    schedule 12.08.2014
comment
@geoand ใช่ฉันเหนื่อยแล้ว ให้ข้อผิดพลาดนี้แก่ฉัน 12 ส.ค. 2557 19:31:19 น. org.springframework.web.servlet.DispatcherServlet noHandlerFound คำเตือน: ไม่พบการแมปสำหรับคำขอ HTTP ด้วย URI [/SpringAngularProject/rest/kfc/brands /sdsd] ใน DispatcherServlet ด้วยชื่อ 'mvc-dispatcher'   -  person Junaid Akhtar    schedule 12.08.2014
comment
ข้อความแสดงว่าคุณได้เพิ่ม SpringAngularProject ใน URL แล้ว คุณแน่ใจหรือว่าคุณทดสอบโดยไม่มีมัน?   -  person geoand    schedule 12.08.2014
comment
ใช่ ฉันเหนื่อยมาหลายครั้งแล้ว แต่ก็ยังใช้งานไม่ได้ :( @geoand   -  person Junaid Akhtar    schedule 12.08.2014
comment
หากคุณดูที่บันทึก Spring จะบอกคุณว่า URL ใดบ้างที่ถูกแมป โปรดดูที่นั่นก่อน หากไม่ได้บันทึกคอนโทรลเลอร์ของคุณจะไม่ถูกสแกนส่วนประกอบ   -  person Bart    schedule 12.08.2014
comment
ไม่มี URL ในบันทึกว่าต้องทำอย่างไรตอนนี้   -  person Junaid Akhtar    schedule 12.08.2014


คำตอบ (1)


ในที่สุดฉันก็ได้คำตอบด้วยตัวเอง แทนที่จะเป็น <context:component-scan base-package="com.controller.HelloWorldController" /> ฉันเปลี่ยนเป็น <context:component-scan base-package="com.controller" /> เราแค่ต้องให้เส้นทางจนถึงแพ็คเกจฐานตามชื่อที่แนะนำ

person Junaid Akhtar    schedule 12.08.2014