Soumali Chatterjee
Soumali Chatterjee

Reputation: 319

How to fix 'HTTP-404' error, during GET request in REST web service using spring boot

My sample spring boot REST web service gives 404 error, and I am not sure what went wrong

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

}

My Request from POSTMan REST request sender: 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>

Response:

{ "timestamp": "2018-12-28T02:48:00.185+0000", "status": 404, "error": "Not Found", "message": "No message available", "path": "/students/stu1/courses/course1" }

Upvotes: 0

Views: 4370

Answers (2)

Soumali Chatterjee
Soumali Chatterjee

Reputation: 319

The issue is resolved, @Component needed to be added to Service class, along with @ComponentScan in the main application class:

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

}

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 40048

As assumed, you have Controller classes in different package com.in28minutes.springboot.controller; and Spring boot main class in different package com.in28minutes.springboot.studentservices;

@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.

use @ComponentScan to scan controller package

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

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

More Info : ref

Upvotes: 3

Related Questions