gpt4 book ai didi

java - Spring Boot Controller 未被映射

转载 作者:行者123 更新时间:2023-12-01 20:00:10 25 4
gpt4 key购买 nike

我正在尝试创建一个基本的 SpringBoot Web 应用程序,其中一个端点名为/movies。我遵循了网站上的一些文档以及有关它的基本 YouTube 教程。

当我从命令行运行 curl localhost:8080 时,应用程序返回“它有效”,但当我尝试访问我的端点/movies 时,我看到“在此服务器上找不到请求的 URL /movies。'

主类:

@SpringBootApplication
public class Main
{
public static void main( String[] args )
{
SpringApplication.run(Main.class, args);
}
}

Controller 类:

@RestController
@RequestMapping("/movies")
public class MovieController {

@Autowired
private MovieService movie_service;

@RequestMapping(method = RequestMethod.GET)
public Collection<Movie> getAllMovies(){
return movie_service.getAllMovies();
}
}

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>com</groupId>
<artifactId>MovieDCU</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.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-tomcat</artifactId>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

对于发布的类似问题,我几乎尝试了该网站上的所有建议答案,但似乎没有任何解决办法。一件不寻常的事情是,当我在命令行中运行应用程序时,我看不到映射:


. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)

2019-11-23 13:27:19.243 INFO 30238 --- [ main] com.Main : Starting Main on Conors-MB with PID 30238 (/Users/eclipse-workspace/Movie/target/classes started by x in /Users/eclipse-workspace/Movie)
2019-11-23 13:27:19.249 INFO 30238 --- [ main] com.Main : No active profile set, falling back to default profiles: default
2019-11-23 13:27:20.011 INFO 30238 --- [ main] com.Main : Started Main in 1.209 seconds (JVM running for 1.611)

感谢任何帮助。

最佳答案

尝试像这样改变你的类(class)。只需在方法级别使用 GetMapping 即可。另请注意,我已将您的服务重命名为标准 Java 命名约定 (camelCase)。

@RestController
public class MovieController {

@Autowired
private MovieService movieService;

@GetMapping("/movies")
public Collection<Movie> getAllMovies(){
return movieService.getAllMovies();
}
}

关于java - Spring Boot Controller 未被映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com