调度程序servlet无法连接

时间:2015-04-16 17:32:15

标签: java xml maven backend

我尝试连接后端服务器,但会出现以下警告,

  

没有找到带有URI [/ FirstApp /]的HTTP请求的映射   名为' rest'

的DispatcherServlet

当我运行我的项目时,任何url请求都没有找到404,下面的代码显示了我的工作
POM.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>edu.karshi</groupId>
    <artifactId>FirstApp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>FirstApp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>FirstApp</finalName>
    </build>

    <properties>
        <spring.version>4.0.2.RELEASE</spring.version>
    </properties>
</project>

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">
    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

rest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="edu.karshi.controllers" />
    <mvc:annotation-driven />
</beans>

控制器

package edu.karshi.controllers;

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/")
@RestController
public class HelloWorldController {
    @RequestMapping(value = "/testMyApp", method = RequestMethod.GET)
    public String test() {
        String result = "Awesome, it works :) ";
        return result;
    }

    @RequestMapping(value = "/testWithParams", method = RequestMethod.GET)
    public String handlePassedParams(
            @RequestParam(value = "name", defaultValue = "no-name") String name,
            @RequestParam(value = "age", defaultValue = "23") int id) {
        if (name == null) {
            name = "don't send me null parameters again!! ";
        }
        String result = " Hello, " + name + " your age is: " + id;
        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

rest-servlet.xml不是spring配置文件。它看起来像一个web.xml文件。

您是否可以将rest-servlet.xml更改为包含所需bean定义的spring配置文件?

相关问题