Comment corriger l'erreur 'HTTP-404', lors d'une requête GET dans le service Web REST à l'aide de Spring Boot


Soumali Chatterjee

Mon exemple de service Web Spring Boot REST donne une erreur 404, et je ne sais pas ce qui s'est mal passé

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);
}

}

Ma demande de l'expéditeur de la demande 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>

Réponse:

{ "timestamp": "2018-12-28T02:48:00.185+0000", "status": 404, "error": "Not Found", "message": "Aucun message disponible", "path": "/ étudiants/stu1/cours/cours1" }

Soumali Chatterjee

Le problème est résolu, @Component devait être ajouté à la classe Service, avec @ComponentScan dans la classe d'application principale :

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);
    }

}

Articles connexes