Reputation: 11
I'm currently learning SpringBoot using Hibernate and PostgreSql. I recently encountered this problem when trying to run my POST request. Any help would be greatly appreciated!
{
"timestamp": "2022-05-26T05:12:32.040+00:00",
"status": 405,
"error": "Method Not Allowed",
"path": "/api/v1/student"
}
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
2022-05-26 01:00:29.650 WARN 68743 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
JSON:
POST http://localhost:8080/api/v1/student
Content-Type: application/json
{
"name": "Bilal",
"email": "[email protected]",
"dob": "1995-12-17"
}
Student.java
package com.example.demo.student;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.Period;
@Entity
@Table
public class Student {
@Id
@SequenceGenerator(name = "student_sequence", sequenceName = "student_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_sequence")
private Long id;
private String name;
private String email;
private LocalDate dob;
@Transient
private Integer age;
public Student() {
}
public Student(Long id, String name, String email, LocalDate dob) {
this.id = id;
this.name = name;
this.email = email;
this.dob = dob;
}
public Student(String name, String email, LocalDate dob) {
this.name = name;
this.email = email;
this.dob = dob;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public LocalDate getDob() {
return dob;
}
public Integer getAge() {
return Period.between(this.dob, LocalDate.now()).getYears();
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", dob=" + dob +
", age=" + age +
'}';
}
}
StudentConfig.java
package com.example.demo.student;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.util.List;
import static java.time.Month.*;
@Configuration
public class StudentConfig {
@Bean
CommandLineRunner commandLineRunner(StudentRepository repository) {
return args -> {
Student mariam = new Student("Mariam", "[email protected]", LocalDate.of(2000, JANUARY, 5));
Student alex = new Student("Alex", "[email protected]", LocalDate.of(2004, JANUARY, 5));
repository.saveAll(List.of(mariam, alex));
};
}
}
StudentController.java
package com.example.demo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("api/v1/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> getStudents() {
return studentService.getStudents();
}
public void registerNewStudent(@RequestBody Student student) {
studentService.addNewStudent(student);
}
}
StudentRepository.java
package com.example.demo.student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}
StudentService.java
package com.example.demo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Service
public class StudentService {
private final StudentRepository studentRepository;
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public List<Student> getStudents() {
return studentRepository.findAll();
}
@PostMapping
public void addNewStudent(Student student) {
System.out.println(student);
}
}
So I added PostMapping to the controller and now I get when I run the POST request for the payload. Any idea?
EDIT: NEVERMIND I GOT IT TO WORK THANK YOU!
Upvotes: 1
Views: 148
Reputation: 56
Actually you don’t have any @PostMapping in your controller so when spring is routing your request it only finds the @GetMapping at the requested path and it’s why it throws a 405 error.
All the Request Mappings should be in a controller, not in a service . You can make many differents controllers if you want.
Upvotes: 2