gpt4 book ai didi

java - 如何使用 Spring Boot 在 REST Web 服务中的 GET 请求期间修复 'HTTP-404' 错误

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

我的示例 spring boot REST Web 服务出现 404 错误,我不确定出了什么问题

package com.in28minutes.springboot.studentservices;
@SpringBootApplication
public class StudentServicesApplication {

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

}

package com.in28minutes.springboot.controller;
@RestController
public class StudentController {

@Autowired
private StudentService studentService;

@GetMapping("/students/{studentId}/courses")
public List<Course> retrieveCoursesForStudent(@PathVariable String
studentId) {
return studentService.retrieveCourses(studentId);
}

@GetMapping("/students/{studentId}/courses/{courseId}")
public Course retrieveDetailsForCourse(@PathVariable String studentId,
@PathVariable String courseId) {
return studentService.retrieveCourse(studentId, courseId);
}

}

来自 POSTMan REST 请求发件人的我的请求: http://localhost:8080/students/stu1/courses/course1

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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot</groupId>
<artifactId>student-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-services</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-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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

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

</project>

回应:

{ "时间戳": "2018-12-28T02:48:00.185+0000", “状态”:404, “错误”:“未找到”, "message": "没有可用的消息", “路径”:“/学生/stu1/类(class)/类(class)1”}

最佳答案

正如假设的,您在不同的包 com.in28minutes.springboot.controller; 中有 Controller 类,在不同的包 com.in28minutes.springboot.studentservices; 中有 Spring boot 主类>

@SpringBootApplication

By default @SpringBootApplication will only scan from the package of the class that declares this annotation.

This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

使用@ComponentScan扫描 Controller 包

@ComponentScan(basePackages = {"com.in28minutes.springboot.controller"})
@SpringBootApplication
public class StudentServicesApplication {

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

更多信息: ref

关于java - 如何使用 Spring Boot 在 REST Web 服务中的 GET 请求期间修复 'HTTP-404' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953272/

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