我的主课是这样的:
package com.ratings.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
System.out.println( "Hello World!" );
}
}
package com.ratings.controller;
@RestController
public class PollController {
@Autowired
Poll poll;
@GetMapping("/")
public String home() {
System.out.println("home " + "HOME");
return "Home";
}
}
和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>com.ratings.main.App</groupId>
<artifactId>Ratings</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Ratings</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.ratings.main.App</mainClass>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我尝试http://localhost:9090/
时得到404。为什么它不返回字符串“ Home”?
答案 0 :(得分:0)
您的SpringBootApplication类在软件包中:
package com.ratings.main;
您的控制器在
package com.ratings.controller;
控制器必须与SpringBootApplication相同或在其下方。
所以将SpringBootApplication移至
com.ratings
或将控制器移至
com.ratings.main.controller
答案 1 :(得分:0)
将您的控制器类放入此软件包com.ratings.main
中。
@SpringApplication
包含@ComponentScan
。该注释从主程序包及其子组中扫描您的bean和配置。
答案 2 :(得分:0)
@SpringBootApplication
软件包:com.ratings.main;
@Controller
软件包:com.ratings.controller;
controller class
必须与用@SpringBootApplication
注释的主类位于同一类或以下。您可以通过添加示例包来解决此问题;
com.ratings.main.*;
相同或以下。
另一方面,您可以将@ComponentScan用于控制器软件包。
@ComponentScan(value = {"your.controller.package"})
@SpringBootApplication
已具有此标签,但仅扫描相同顺序及以下的顺序。