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

标签 java rest spring-boot

我的示例 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/

相关文章:

java - 在 Spring Rest Controller 中出现 JSON 解析错误 (MismatchedInputException)

python - picloud 上发布的函数抛出错误

java - 更好的 Elasticsearch 客户端从 JAVA Spring-boot 连接 AWS Elasticsearch

java - IntelliJ 想法 : build and run Java apps in docker containers

Java泛型类型比较

java - 在我的 route 获取要处理的条目的正确方法是什么

json - 使用JSON文件的JAX-RS生成错误415

javascript - 必需的字符串参数 'text' 不存在

java - 这段java代码是线程安全的吗?

java - 在 Java 中使用各种日历时区(不使用 Joda Time)