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

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

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:

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.

答案1

得分: 3

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

@Autowired
private JSONObject json;

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

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

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);

答案2

得分: 2

Use the following code snippet to resolve the issue,

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

Use the following code snippet to resolve the issue,

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:

确定