Spring Boot:应用程序显示404错误

时间:2018-08-11 06:03:01

标签: java spring spring-mvc spring-boot

我正在尝试运行Spring Boot REST API应用程序,但出现404错误。

package com.spring.boot.entry;
@SpringBootApplication
@ComponentScan(basePackageClasses = HelloController.class)
public class CourseApiApp {
    public static void main(String[] args) {
        SpringApplication.run(CourseApiApp.class, args);
    }
}

package com.spring.boot.entry.hello;
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello() {
        return "Hi First Spring boot application ";
    }
}

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>io.spring.boot.quickstart</groupId>
  <artifactId>course-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring Boot API</name>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
  </parent>

  <dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-solr</artifactId>
    </dependency>
  </dependencies>

  <properties>
    <java.version>1.8</java.version>
  </properties>
</project>

我也尝试过@ComponentScan(basePackageClasses = HelloController.class), 但没有运气。您能帮忙解决这个问题吗?

如果我点击http://localhost:8080/hello网址,那么我会得到404。

1 个答案:

答案 0 :(得分:3)

我在系统上执行了代码,除非您弄乱了目录结构,否则它会按预期工作。交叉检查一次。

enter image description here

CourseApiApp.java

enter image description here

HelloController.java

enter image description here

输出

enter image description here