HTTP状态404 - 未找到Apache Tomcat / 8.5.16

时间:2017-07-18 09:11:38

标签: java spring macos maven tomcat

尝试使用JavaEE Spring Framework设置一个简单的hello world应用程序,但在部署.war文件后遇到上述问题。

我在网上经历了一些资源但没有成功。

这是我的app初始化程序类;

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[]{AppConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[]{AppConfig.class};
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/demoapp/*"};
}
}

这就是pom.xml;

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>demoapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <java.version>1.8</java.version>
    <spring.version>4.3.9.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</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>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.0.0.RELEASE</version>
    </dependency>

</dependencies>

<build>
    <finalName>demoapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

我运行mvn clean instaal然后从目标目录中选择.war文件,然后使用“Manager App”选项将其部署到tomcat服务器。

但是,当我访问链接http://localhost:8080/demoapp/时,我会看到以下页面;

enter image description here

我的文件夹结构如下;

enter image description here

控制器

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Lawrence on 17/07/2017.
*/
@Controller
public class HelloController {

  @RequestMapping("/hi")
  public String hi() {
    return "Hello World";
  }
}

对于如何解决此问题的一些帮助或指导,将受到高度赞赏。

由于

1 个答案:

答案 0 :(得分:0)

404错误是因为服务器无法找到/ demoapp /,你可以将控制器发布到已映射url / demoapp /的地方。