【问题标题】:How to fix 'HTTP-404' error, during GET request in REST web service using spring boot如何在使用 Spring Boot 的 REST Web 服务中的 GET 请求期间修复“HTTP-404”错误
【发布时间】:2019-05-25 23:14:55
【问题描述】:

我的示例 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": "没有可用的消息", “路径”:“/students/stu1/courses/course1” }

【问题讨论】:

  • 我尝试运行您的代码,但没有收到 404。您确定此应用程序托管在 8080 上吗?
  • 你能发布堆栈跟踪和你的 pom.xml 吗?
  • 用 pom.xml 和 http 响应更新了帖子。
  • @SoumaliChatterjee 你能直接从 chrome 调用 localhost:8080/students/stu1/courses/course1 吗?
  • 您的server.port=8080 在应用程序属性中吗?

标签: java rest spring-boot


【解决方案1】:

问题已解决,需要将@Component 添加到Service 类中,同时在主应用程序类中添加@ComponentScan:

package com.in28minutes.springboot.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import com.in28minutes.springboot.model.Course;
import com.in28minutes.springboot.model.Student;

@Component
public class StudentService {

public List<Course> retrieveCourses(String studentId) {
    Map<String, Course> courses = Student.getStudentObj(studentId).getCourses();
    List<Course> courseList = 
    courses.values().parallelStream().collect(Collectors.toList());
    return courseList;
 }

 public Course retrieveCourse(String studentId, String courseId) {
    return Student.getStudentObj(studentId).getCourses().get(courseId);
 }

}

package com.in28minutes.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication 
@ComponentScan("com.in28minutes.springboot")
public class StudentServicesApplication {

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

}

【讨论】:

    【解决方案2】:

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

    @SpringBootApplication

    默认@SpringBootApplication只会从声明这个注解的类的包中扫描。

    这是一个方便的注解,相当于声明@Configuration、@EnableAutoConfiguration 和@ComponentScan。

    如果没有定义特定的包,则从声明该注解的类的包开始扫描。

    使用@ComponentScan扫描控制器包

    @ComponentScan(basePackages = {"com.in28minutes.springboot.controller"})
     @SpringBootApplication
     public class StudentServicesApplication {
    
     public static void main(String[] args) {
     SpringApplication.run(StudentServicesApplication.class, args);
         }
      }
    

    更多信息: ref

    【讨论】:

    • 我打算建议 OP 将他启动日志中的“映射”行添加到他的帖子中。但是您首先收到了关于“包裹”不匹配的回复。你完全正确!我正在祈祷它解决了这个问题:)
    • 谢谢,它应该可以解决问题,因为很明显他的包没有组织@paulsm4
    • 感谢您的精彩跟进。解决了这个问题,如下所示。
    • 谢谢!这对我帮助很大。
    猜你喜欢
    • 2019-05-30
    • 2014-12-14
    • 2020-10-07
    • 2019-10-06
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多