Home > Mobile >  Why am I receiving a "Request method 'POST' not supported" ? I get error code 40
Why am I receiving a "Request method 'POST' not supported" ? I get error code 40

Time:06-28

Postman error

Controller:

@RestController

@RequestMapping("/student_enrollment/class_subject")

public class ClassController {

private ClassService classService;
public ClassController(ClassService classService) {
    super();
    this.classService = classService;
}

@PostMapping()

public ResponseEntity<Classes> saveClass(@RequestBody Classes classes) {
    return new ResponseEntity<Classes>(classService.saveClass(classes), 
    HttpStatus.CREATED);
}

@GetMapping
public List<Classes> getAllClasses() {
    return classService.getAllClasses();
}

model:

@Data

@Entity

@Table(name = "class_subject")

public class ClassSubject {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

private int subject_id;

@Column(value = "prerequisite")
private String prerequisite;

@Column(value = "max_capacity")
private int max_capacity;

}

service impl:

@Service public class ClassServiceImpl implements ClassService{

@Override

public ClassSubject updateClassSubject(ClassSubject classSubject, int classSubjectId) {
    
    ClassSubject existingClassSubject = classSubjectRepository.findById(classSubjectId).orElseThrow(() -> new ResourceNotFoundException("ClassSubject", "classSubjectId", classSubjectId));
    
    existingClassSubject.setSubject_id(classSubject.getSubject_id());
    existingClassSubject.setPrerequisite(classSubject.getPrerequisite());
    existingClassSubject.setMax_capacity(classSubject.getMax_capacity());
    
    classSubjectRepository.save(existingClassSubject);
    
    return existingClassSubject;
}

Service:

public interface ClassService {

Classes saveClass(Classes classes);

List<Classes> getAllClasses();

Classes getClassByID(int classId);

Classes updateClass(Classes classes, int classId);

void deleteClass(int classId);

}

SQL Queries:

CREATE TABLE class_subject(

subject_id INT NOT NULL,
prerequisite VARCHAR(30),
max_capacity INT NOT NULL,

PRIMARY KEY (subject_id)

);

CodePudding user response:

Because in Postman you are using PUT method, not the POST method.

CodePudding user response:

Let me guess you are expecting 'subject_id' to be auto-generate using database sequence. Then you are missing two annotations @GeneratedValue and @SequenceGenerator.

As 'subject_id' is not an optional field. That's why you getting the error "Field 'subject_id' doesn't have a default value".

To create a relationship between the 'subject_id' field and DB sequence use the following.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@SequenceGenerator(name="seq",sequenceName="sequence_in_db")        
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")   
private int subject_id;
  

You can find more details on this topic what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?

  • Related