我正在从事Spring Boot项目,
我正在尝试从邮递员发送获取请求,但我收到500个内部服务器错误。
请帮助我改进代码。
控制器:
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:4200")
public class sessionMcqController {
@Autowired
private sessionMcqService service;
@GetMapping("/viewmcq")
public List<Question> getAll(){
return service.getAll();
}
}
型号:
1。问题模型:
@Entity
@Table(name = "mcq_question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JoinColumn (name = "questionId")
private Long questionId;
@Column(nullable = false)
private String question;
@OneToOne(fetch = FetchType.LAZY, targetEntity = Option.class)
@JoinTable( name = "mcq_answer", joinColumns = @JoinColumn ( name = "question_id"), inverseJoinColumns = @JoinColumn( name = "option_id"))
private List <Option> options;
@OneToMany(mappedBy = "questionId", cascade = CascadeType.ALL)
private Set<Option> option;
public Question() { }
public Long getMcqId() {
return questionId;
}
public void setMcqId(Long mcqId) {
this.questionId = mcqId;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
@Override
public String toString() {
return "McqModel{" +
"mcqId=" + questionId +
", question='" + question + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Question question1 = (Question) o;
return questionId.equals(question1.questionId) &&
question.equals(question1.question) &&
options.equals(question1.options) &&
option.equals(question1.option);
}
@Override
public int hashCode() {
return Objects.hash(questionId, question, options, option);
}
}
2。 OptionModel:
@Entity
@Table(name = "mcq_Options")
public class Option {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "option_id")
private int optionId;
private String option;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn
private Question questionId;
public Option() { }
public Option(String option, Question questionId) {
this.option = option;
}
public int getOptionId() {
return optionId;
}
public void setOptionId(int optionId) {
this.optionId = optionId;
}
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
public Question getQuestionId() {
return questionId;
}
public void setQuestionId(Question questionId) {
this.questionId = questionId;
}
@Override
public String toString() {
return "Option{" +
"optionId=" + optionId +
", option='" + option + '\'' +
", questionId=" + questionId +
'}';
}
}
存储库: 1.QuestionRepository
@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
}
2.OptionRepository
@Repository
public interface OptionRepository extends JpaRepository<Option, Integer> {
}
服务:
@Service
public class sessionMcqService {
@Autowired
private QuestionRepository repository;
public List<Question> getAll(){
return repository.findAll();
}
}
错误:
java.lang.NullPointerException:空 在com.bluepi.TrainingProject.controller.sessionMcqController.getAll(sessionMcqController.java:22)〜[classes /:na] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处〜[na:1.8.0_201] 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_201] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_201] 在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_201] 在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)〜[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)〜[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:634)〜[tomcat-embed-core-9.0.16.jar:9.0.16] 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)〜[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)〜[tomcat-embed-core-9.0.16.jar:9.0.16]
答案 0 :(得分:1)
您将需要在@Autowired
的{{1}}行上方添加private sessionMcqService service;
。