QuestionController Class:
package app.controller;
import java.util.List;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import app.model.Question;
import app.repository.QuestionRepository;
/**
* @implNote Controller Class to manage Questions http operations
* @author usardar
*
*/
@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {
@Autowired
private JSONObject json;
@Autowired
private QuestionRepository questionRepository;
@Autowired
private Question question;
/*
* Usage: Example: http://localhost:8080/question/retrievequestions
* Output: [ {"qId":3, "title":"Who is the founder of Dawn news?", "optA":"Quaid-E-Azam", "optB":"Usama", "optC":"Ali Hassan", "answer":"Quaid-E-Azam"} ]
*/
@GetMapping("/retrievequestions")
public List<Question> getQuestionsList() {
List<Question> questionsList = questionRepository.findAll();
if (questionsList != null) {
return questionsList;
}
return questionsList;
}
/*
* Usage:
* Example: http://localhost:8080/question/addquestion
* Post Request : {"title":"Who is the founder of Pakistan?", "optA":"Quaid-E-Azam", "optB":"Usama", "optC":"Ali Hassan", "answer":"Quaid-E-Azam"}
* Output: [ {"success", "Question Inserted Successfuly"} ]
*/
@PostMapping("/addquestion")
public String addQuestion(@RequestBody String newQuestionObj) {
System.out.println(newQuestionObj);
// json = new JSONObject(newQuestionObj);
question.setQuestion(json.getString("newQuestion"));
question.setOptA(json.getString("newOptA"));
question.setOptB(json.getString("newOptB"));
question.setOptC(json.getString("newOptC"));
question.setAnswer(json.getString("newAnswer"));
questionRepository.save(question);
json.clear();
json.put("success", "Question Inserted Successfuly");
return json.toString();
}
}
This is my class for reference.
Error message: Field json in app.controller.QuestionController required a bean of type 'org.json.JSONObject' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action:
Consider defining a bean of type 'org.json.JSONObject' in your configuration.
You are trying to inject a class that is not a Spring Bean:
@Autowired
private JSONObject json;
As in the commented out code simply instantiate the object:
JSONObject json = new JSONObject(newQuestionObj);