java - 我尝试使用@GetMapping 和@RequestMapping 对其进行映射,但仍然无法找到解决方案
问题描述
我用这段代码得到 404。
控制器类
@RestController
public class TestAPI {
@RequestMapping(method = RequestMethod.GET, value = "/")
public ResponseEntity<String> hello() {
System.out.println("Hit me!");
return new ResponseEntity<String>("Hello, you!", HttpStatus.OK);
}
}
应用起点:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
dispatcherServlet 在 localhost 上达到 8080 时正在初始化,但它没有调用 get 方法。错误为 404,未找到。我尝试使用@GetMapping 和@RequestMapping 对其进行映射,但仍然找不到解决方案。
解决方案
推荐阅读
- android - 弹出片段后,所有 Editexts 都将替换为相同的值
- python - Pytorch 通过知识转移保存和加载 VGG16
- algorithm - 如何从执行时间验证算法的时间复杂度?
- excel - EXCEL COUNTIFS 未正确计算比较 - 返回 0
- c++ - 10.8 == 10.8 评估为假?
- c# - 如何在asp.net web api中对请求进行排队并在服务器端执行?
- reactjs - 为 React 自定义 AWS Amplify 身份验证 UI
- android - 条件计算
- javascript - django中服务器发送验证失败时如何防止表单提交?
- python - 如何正确比较两个日期时间变量?