Spring Boot错误404找不到GetMapping



我是弹簧引导的新手,正在尝试使用RestController创建API端点。当我试图调用端点时,它会给我一个错误404,找不到,导致浏览器中显示While标签错误页面

应用类别:

package com.guider.guiderfinder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.guider")
public class GuiderFinderApplication {
public static void main(String[] args) {
SpringApplication.run(GuiderFinderApplication.class, args);
}
}

控制器类别:

package com.guider.guiderfinder.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.guider.guiderfinder.model.Guide;
import com.guider.guiderfinder.repository.GuideRepository;
@RestController
@RequestMapping("/api/v1/") //Standard url endpoint defined
public class GuideController {

@Autowired
private GuideRepository guideRepository;

@GetMapping("/guides") //this api endpoint is called from url = localhost:8080/api/v1/guides
public List<Guide> getAllGuides() {
System.out.println("API was hit");
return guideRepository.findAll();
}
}

pox.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.guider</groupId>
<artifactId>guider-finder</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>guider-finder</name>
<description>Backend of guide finder</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-starter-data-jpa</artifactId>
</dependency>

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

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sourceforge.javydreamercsw/MySQL-Driver -->
<dependency>
<groupId>net.sourceforge.javydreamercsw</groupId>
<artifactId>MySQL-Driver</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

当您编写@ComponentScan("com.guider")时,您告诉Spring只在此目录中扫描。您的控制器位于子目录中。使用@ComponentScan(basePackages = "com.guider")应该可以解决您的问题。另一种选择是添加要扫描的其他包。@ComponentScan({"com.guider", "com.guider.guiderfinder"})就是一个例子。

最新更新