Spring mvc编译正确,但在服务器上部署时给出了404错误

时间:2015-08-24 06:31:32

标签: spring rest spring-mvc servlets controller

我是spring mvc的初学者,并在一些在线教程后创建了我自己的应用程序。我按照在线教程在java应用程序中使用jackson mapper,它运行良好。然后我进一步尝试修改它并将其更改为spring mvc项目,以便在用户从Web调用url时发送json数据。

如果我通过与eclipse集成的tomcat服务器运行应用程序,它可以正常工作。但是,当我手动将war文件复制/粘贴到tomcat / webapps文件夹并从bin / tomcat7.exe启动tomcat时,它会给我一个404错误。为什么它会给我404错误,我该如何解决?

请告诉我这里可能做错了什么,这将有助于我了解更多有关spring mvc的信息,谢谢。

PS:我知道stackoverflow关于这个问题有类似的问题,我试着按照它们来解决这个问题,但是没有成功

这是我的代码:

的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_2_5.xsd"
    version="2.5">

    <display-name>Sample Spring Maven Project</display-name>

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

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


</web-app>

弹簧-config.xml中

<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"
   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">

   <context:component-scan base-package="com.tutorialspoint" />
<mvc:annotation-driven/>  

</beans>

HelloController.java

package com.tutorialspoint;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello() throws JsonGenerationException, JsonMappingException, IOException {

       JsonDemo jsonDemo = new JsonDemo();
       return jsonDemo.objectToJson();

   }

}

的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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>

      <groupId>HelloWorld</groupId>
      <artifactId>HelloworldProject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>

      <name>HelloworldProject</name>
      <url>http://maven.apache.org</url>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>

     <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-mapper-asl</artifactId>
       <version>1.9.13</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>


      </dependencies>
    </project>

1 个答案:

答案 0 :(得分:0)

我想提及您在项目中遇到的几个问题:

1)请使用这个:https://www.springframework.org/schema/beans/spring-beans-4.0.xsd

可以使用<context:component-scan base-package="com.tutorialspoint" /><mvc:annotation-driven/>,但您使用的是Spring 4.2.0,但您的配置文件使用的是旧的3.0 xsd。

为了帮助您处理Spring配置,我将在此处发布:

<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"
   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">

   <context:component-scan base-package="com.tutorialspoint" />
   <mvc:annotation-driven/>
</beans>

2)我强烈建议您迁移到servlet 3.0 / 3.1。基本上它意味着更改web.xml标题:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
 <display-name>Sample Spring Maven Project</display-name>
 <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>/</url-pattern>
 </servlet-mapping>

 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-config.xml</param-value>
 </context-param>

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

请注意一些重要的变化:

  • 提供了一个init-param我们正在为你的Spring Servlet使用context-param。
  • 侦听器,它创建所有Servlet和过滤器共享的Spring容器

3)在pom.xml中添加一些其他依赖项:

   <!-- Spring 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>

这可以帮助你处理mvc。

如果您可以测试所有这些并提供完整的catalina日志,包括部署,这将有助于解决问题。

4)控制器类:

package com.tutorialspoint;

import java.io.IOException;

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.RestController;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

@RestController
public class HelloController{

   @RequestMapping(value = "/hello", method = RequestMethod.GET)
   public String printHello() throws JsonGenerationException, JsonMappingException, IOException {

       JsonDemo jsonDemo = new JsonDemo();
       return jsonDemo.objectToJson();

   }

}