I tried sending this in POSTMAN:
{
"title": "the",
"type": "MCQ",
"teacher_id": "2",
"questions" : [{
"content": "Who are you",
"answers":[{
"content": "you",
"correct" : "true"
}]
}],
"status": "OPEN"
}
Tried switching media types but it didn’t work, I couldn’t send anything to that mapping successfully, I think it might have to do with the questions list that also has an answers list.
Get the error: "error": "Unsupported Media Type", "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8'
- Here’s the entity:
package com.academy.exams.entity;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
@Entity
@Table(name = "Exam")
public class Exam {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "exam_id")
private Long examId;
private String title;
@Enumerated(EnumType.STRING)
private ExamType type;
@JsonManagedReference
@OneToMany(mappedBy = "exam", cascade = CascadeType.ALL)
private List<Question> questions;
@Column(name = "teacher_id")
private Long teacherId;
private String status;
public Long getId() {
return examId;
}
public void setId(Long examId) {
this.examId = examId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ExamType getType() {
return type;
}
public void setType(ExamType type) {
this.type = type;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public Long getTeacherId() {
return teacherId;
}
public void setTeacherId(Long teacherId) {
this.teacherId = teacherId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@PostConstruct
public void init() {
System.out.println("Exam initialized");
}
}
- Here’s the service, knowing I used this
@Repository public interface ExamRepository extends JpaRepository<Exam, Long> {}
:
public Exam CreateExam(Exam exam){
return examRepository.save(exam);
}
- And here’s the controller:
package com.academy.exams.controller;
import com.academy.exams.entity.Exam;
import com.academy.exams.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/exams")
public class ExamController {
@Autowired
private ExamService examService;
@PostMapping("/create")
public Exam createExam(@RequestBody Exam exam){
return examService.CreateExam(exam);
}
}
- Here’s the question Entity too:
package com.academy.exams.entity;
import com.academy.exams.entity.Exam;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import java.util.List;
@Entity
@Table(name = "Question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "question_id")
private Long questionId;
@Column(name = "content")
private String content;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "exam_id")
private Exam exam;
@JsonBackReference
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
private List<Answer> answers;
public Long getId() {
return questionId;
}
public void setId(Long id) {
this.questionId = questionId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Exam getExam() {
return exam;
}
public void setExam(Exam exam) {
this.exam = exam;
}
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}
- And the answers entity:
package com.academy.exams.entity;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
@Entity
@Table(name = "Answer")
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="answer_id")
private Long answerId;
private String content;
@Column(name="correct")
private Boolean correct;
@JsonManagedReference
@ManyToOne
@JoinColumn(name = "question_id" )
private Question question;
public Long getId() {
return answerId;
}
public void setId(Long answerId) {
this.answerId = answerId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Boolean getCorrect() {
return correct;
}
public void setCorrect(Boolean correct) {
this.correct = correct;
}
}
New contributor
JaCoffee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.