即使Tomcat服务器似乎已初始化并在localhost:8080中运行,我仍收到“ HTTP状态404 –未找到”。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-consuming-rest</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>


控制器是

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

     @RequestMapping("/")
        public String hello(){
            return "Hello!";

     }
}


主要应用代码是

package com.example.demo;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class HelloWorldApplication {




    public static void main(String[] args) {


        SpringApplication.run(HelloWorldApplication.class, args);
    }


}


控制台日志:

2019-08-09 19:53:07.161  INFO 13948 --- [           main] com.example.demo.HelloWorldApplication   : Starting HelloWorldApplication on DESKTOP-BIMC3QL with PID 13948 (C:\Users\AdharshD\Documents\workspace-sts-3.9.9.RELEASE\HelloWorld\target\classes started by AdharshD in C:\Users\AdharshD\Documents\workspace-sts-3.9.9.RELEASE\HelloWorld)
2019-08-09 19:53:07.164  INFO 13948 --- [           main] com.example.demo.HelloWorldApplication   : No active profile set, falling back to default profiles: default
2019-08-09 19:53:07.791  INFO 13948 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-08-09 19:53:07.810  INFO 13948 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-09 19:53:07.810  INFO 13948 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-08-09 19:53:07.891  INFO 13948 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-09 19:53:07.891  INFO 13948 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 679 ms
2019-08-09 19:53:08.076  INFO 13948 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-08-09 19:53:08.079  INFO 13948 --- [           main] com.example.demo.HelloWorldApplication   : Started HelloWorldApplication in 1.211 seconds (JVM running for 1.855)

最佳答案

您缺少spring-boot-starter-web工件。 Artifact contains告诉Spring Boot扫描软件包(存在主要方法的软件包)及其所有子软件包,以进行配置,组件和控制器的扫描。由于缺少此工件,spring无法注册您的控制器,并抛出404-Not found。

10-07 19:25
查看更多