This error means “org.json.JSONObject” 是什么意思?

huangapple go评论98阅读模式
英文:

what this error means 'org.json.JSONObject'?

问题

以下是您提供的代码的翻译部分:

错误消息:
app.controller.QuestionController中的字段"json"需要一个类型为'org.json.JSONObject'的bean,但找不到该bean。

注入点具有以下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

操作:
考虑在您的配置中定义一个类型为'org.json.JSONObject'的bean。

英文:

QuestionController Class:

  1. package app.controller;
  2. import java.util.List;
  3. import org.json.JSONObject;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.CrossOrigin;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import app.model.Question;
  12. import app.repository.QuestionRepository;
  13. /**
  14. * @implNote Controller Class to manage Questions http operations
  15. * @author usardar
  16. *
  17. */
  18. @RestController
  19. @RequestMapping("/question")
  20. @CrossOrigin
  21. public class QuestionController {
  22. @Autowired
  23. private JSONObject json;
  24. @Autowired
  25. private QuestionRepository questionRepository;
  26. @Autowired
  27. private Question question;
  28. /*
  29. * Usage: Example: http://localhost:8080/question/retrievequestions
  30. * Output: [ {"qId":3, "title":"Who is the founder of Dawn news?", "optA":"Quaid-E-Azam", "optB":"Usama", "optC":"Ali Hassan", "answer":"Quaid-E-Azam"} ]
  31. */
  32. @GetMapping("/retrievequestions")
  33. public List<Question> getQuestionsList() {
  34. List<Question> questionsList = questionRepository.findAll();
  35. if (questionsList != null) {
  36. return questionsList;
  37. }
  38. return questionsList;
  39. }
  40. /*
  41. * Usage:
  42. * Example: http://localhost:8080/question/addquestion
  43. * Post Request : {"title":"Who is the founder of Pakistan?", "optA":"Quaid-E-Azam", "optB":"Usama", "optC":"Ali Hassan", "answer":"Quaid-E-Azam"}
  44. * Output: [ {"success", "Question Inserted Successfuly"} ]
  45. */
  46. @PostMapping("/addquestion")
  47. public String addQuestion(@RequestBody String newQuestionObj) {
  48. System.out.println(newQuestionObj);
  49. // json = new JSONObject(newQuestionObj);
  50. question.setQuestion(json.getString("newQuestion"));
  51. question.setOptA(json.getString("newOptA"));
  52. question.setOptB(json.getString("newOptB"));
  53. question.setOptC(json.getString("newOptC"));
  54. question.setAnswer(json.getString("newAnswer"));
  55. questionRepository.save(question);
  56. json.clear();
  57. json.put("success", "Question Inserted Successfuly");
  58. return json.toString();
  59. }
  60. }

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.

答案1

得分: 3

你正在尝试注入一个不是Spring Bean的类:

  1. @Autowired
  2. private JSONObject json;

就像注释掉的代码一样,只需实例化对象:

  1. JSONObject json = new JSONObject(newQuestionObj);
英文:

You are trying to inject a class that is not a Spring Bean:

  1. @Autowired
  2. private JSONObject json;

As in the commented out code simply instantiate the object:

  1. JSONObject json = new JSONObject(newQuestionObj);

答案2

得分: 2

Use the following code snippet to resolve the issue,

  1. JSONObject json = new JSONObject(newQuestionObj);
英文:

Use the following code snippet to resolve the issue,

  1. JSONObject json = new JSONObject(newQuestionObj);

huangapple
  • 本文由 发表于 2023年6月26日 16:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定