Why Do I Get 404 for Rest With Spring-Boot

Why do I get 404 for rest with spring-boot

The first thing I would try is to put @RequestMapping("/") on the class definition of the controller. Keep the same value on the method.

Another thing, unrelated to your problem, is that you do not need to define that custom query. JPA is actually smart enough to do the query you defined just by using that method name. Check out the findByLastName example here: https://spring.io/guides/gs/accessing-data-jpa/.

Why do I get 404 Not Found in Spring Boot

Ok, so the issue was very silly, and the solution is super simple:

I was using @Controller adnotation in my RfidReaderController which is from springframework.stereotype library. I changed it to @RestController and now it works.
just as follows:

@Controller
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/rfid")
public class RfidReaderController {
...

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

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

}


Related Topics



Leave a reply



Submit