我的项目由以下四个表组成:
我必须基于多个标签生成问题
或者简单地返回所有问题的列表(当没有提供标签时)
或者将用户提供的答案插入到Question_Answer表中。
控制器类SpringServiceController。java(生成JSON格式的结果)如下:
package com.bargadss.SpringService.Controller;
import java.text.ParseException;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.bargadss.SpringService.DAO.QuestionService;
import com.bargadss.SpringService.Domain.*;
@RestController
@RequestMapping("/service/question/")
public class SpringServiceController {
QuestionService questionService=new QuestionService();
@RequestMapping(value = "/{tag1},{tag2},{tag3}", method = RequestMethod.GET,headers="Accept=application/json")
public List<Questions> getQuestions(@PathVariable("tag1") String tag1, @PathVariable("tag2") String tag2, @PathVariable("tag3") String tag3) {
List<Questions> Qobj=questionService.getQuestionByTag(tag1, tag2, tag3);
return Qobj;
}
@RequestMapping(method = RequestMethod.GET,headers="Accept=application/json")
public List<Questions> getAllQuestions() {
List<Questions> Qobj=questionService.getAllQuestion();
return Qobj;
}
@RequestMapping(value="/insert/{user_id}/{question_id}/{answer}",method = RequestMethod.POST,headers="Accept=application/json")
public List<Questions> addQuestions(@PathVariable int user_id,@PathVariable int question_id,@PathVariable String answer) throws ParseException {
Question_Answer qtnAns = new Question_Answer();
qtnAns.setUser_id(user_id);
qtnAns.setQuestion_id(question_id);
qtnAns.setAnswer(answer);
questionService.insertAnswer(qtnAns.getUser_id(), qtnAns.getQuestion_id(), qtnAns.getAnswer());
return questionService.getAllQuestion();
}
}
列表问题控制器.java使用Spring REST 三重板 ( 生成 JSP 页面 ) 如下:
package com.bargadss.SpringService.Controller;
import java.util.LinkedHashMap;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import com.bargadss.SpringService.Domain.*;
@Controller
public class ListQuestionController {
@RequestMapping("/listQuestion/{tag1},{tag2},{tag3}")
public ModelAndView listQuestions(@PathVariable("tag1") String Tag1, @PathVariable("tag2") String Tag2, @PathVariable("tag3") String Tag3) {
RestTemplate restTemplate = new RestTemplate();
String url="http://localhost:8080/FetchQuestions/service/question/{tag1},{tag2},{tag3}";
List<LinkedHashMap> Qobj=restTemplate.getForObject(url, List.class,Tag1,Tag2,Tag3);
return new ModelAndView("listQuestion", "questions", Qobj);
}
@RequestMapping("/listAllQuestion/")
public ModelAndView listAllQuestion() {
RestTemplate restTemplate = new RestTemplate();
String url="http://localhost:8080/FetchQuestions/service/question/";
List<LinkedHashMap> Qobj=restTemplate.getForObject(url, List.class);
return new ModelAndView("listQuestion", "questions", Qobj);
}
@RequestMapping("/insertQuestionAnswer/{user_id}/{qtn_id}/{answer}")
public ModelAndView insertQuestionAnswer(@PathVariable("user_id") String user_ID,
@PathVariable("qtn_id") String qtn_ID, @PathVariable("answer") String answer) {
RestTemplate restTemplate = new RestTemplate();
String url="http://localhost:8080/FetchQuestions/service/question/insert/{user_id}/{qtn_id}/{answer}";
List<LinkedHashMap> Qobj=restTemplate.getForObject(url, List.class,user_ID,qtn_ID,answer);
return new ModelAndView("listQuestion", "questions", Qobj);
}
}
问题服务.java类按如下方式执行 DAO 活动:
package com.bargadss.SpringService.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.bargadss.SpringService.Domain.*;
import com.bargadss.SpringService.Utility.DBUtility;
public class QuestionService {
private Connection connection;
public QuestionService() {
connection = DBUtility.getConnection();
}
public void insertAnswer(int userId,int qtnId,String answer){
//Question_Answer objQA = new Question_Answer();
PreparedStatement preparedStatement = null;
try{
preparedStatement = connection.prepareStatement("insert into question_answer values (?,?,?)");
preparedStatement.setInt(1, userId);
preparedStatement.setInt(2, qtnId);
preparedStatement.setString(3, answer);
int result = preparedStatement.executeUpdate();
if(result == 0)
System.out.println("INSERTION OF DATA FAILED");
} catch (SQLException e){
e.printStackTrace();
}
finally{
try { if (preparedStatement != null) preparedStatement.close(); } catch (Exception e) {System.out.println("Exception Closing The Prepared Statement");e.printStackTrace();};
}
}
public List<Questions> getAllQuestion(){
Questions objQ = new Questions();
ResultSet rs = null;
PreparedStatement preparedStatement = null;
List<Questions> qtns = new ArrayList<Questions>();
try {
preparedStatement = connection.prepareStatement("select * from questions");
rs = preparedStatement.executeQuery();
while(rs.next()) {
objQ.setQuestion_id(Integer.parseInt( rs.getString("Question_id") ) );
objQ.setQuestion_text(rs.getString("Question_Text"));
qtns.add(objQ);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try { if (rs != null) rs.close(); } catch (Exception e) {System.out.println("Exception Closing The result Set");e.printStackTrace();};
try { if (preparedStatement != null) preparedStatement.close(); } catch (Exception e) {System.out.println("Exception Closing The Prepared Statement");e.printStackTrace();};
/*try { if (connection != null) connection.close(); } catch (Exception e) {System.out.println("Exception Closing The Connection");e.printStackTrace();};*/
}
return qtns;
}
public List<Questions> getQuestionByTag(String Tag1,String Tag2,String Tag3){
Questions objQ = new Questions();
ResultSet rs = null;
PreparedStatement preparedStatement = null;
List<Questions> qtns = new ArrayList<Questions>();
try {
preparedStatement = connection.
prepareStatement("select questions.Question_id,questions.Question_Text from questions,questiontype " +
"where questions.Question_id=questiontype.Question_id " +
"and questiontype.Tag in (?,?,?)");
preparedStatement.setString(1, Tag1);
preparedStatement.setString(2, Tag2);
preparedStatement.setString(3, Tag3);
rs = preparedStatement.executeQuery();
while(rs.next()) {
objQ.setQuestion_id(Integer.parseInt( rs.getString("Question_id") ) );
objQ.setQuestion_text(rs.getString("Question_Text"));
qtns.add(objQ);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try { if (rs != null) rs.close(); } catch (Exception e) {System.out.println("Exception Closing The result Set");e.printStackTrace();};
try { if (preparedStatement != null) preparedStatement.close(); } catch (Exception e) {System.out.println("Exception Closing The Prepared Statement");e.printStackTrace();};
/*try { if (connection != null) connection.close(); } catch (Exception e) {System.out.println("Exception Closing The Connection");e.printStackTrace();};*/
}
return qtns;
}
}
在执行项目(阿帕奇雄猫7.0.12)时,使用以下URL,它生成HTTP状态500
http://localhost:8080/FetchQuestions/insertQuestionAnswer/3/9/True
此外,当我执行以下 URL 时,它会生成 HTTP 状态 405 - 请求方法“GET”不受支持
http://localhost:8080/FetchQuestions/service/question/insert/3/9/True
我做错了什么?我错过了什么吗?
HTTP 状态 500 的堆栈跟踪如下所示:
严重: Servlet.service() 在上下文中与路径 [/FetchQuestions] 抛出异常 [请求处理失败; 嵌套异常是组织.springframework.web.client.HttpClient错误异常: 405 方法不允许] 与根本原因组织.springframework.web.client.客户端.客户端.405 方法.java不允许handleResponseError(RestTemplate.java:576) at org.springframework.web.client.restTemplate.doExecute(RestTemplate.java:532) at org.springframework.web.client.resttemplate.execute(RestTemplate.java:489) at org.springframeworkwork.web.client.restTemplate.getForObject(RestTemplate.java:226) 在 com.bargadss.SpringService.Controller.ListQuetionController.insertQueswer(ListQuetionController.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeContss或调用0(Native方法) 在 太阳.反射.本地方法访问执行调用(本机方法访问执行.java:57) 在 sun.反射.委派方法访问命令调用(委派方法访问命令.java:43) 在 java.lang.reflect.方法.invoke(方法.java:606) 在组织.springframework.web.方法.支持.可调用的Handler方法调用(可调用的Handler方法.java:214) 在 org.springframework.web.method.support.可调用的Handler方法.java:132)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleinternal(请求映射Handleradapter.java:689) at在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) at org.springframework.web.servlet.DispatcherServlet.doDispatch(调度员Servlet.java:945) at org.springframework.web.servlet.servlet.doService(调度员Servlet.java:876) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822) at javax.servlet.http.Servlet.service(httpServlet.java:621) at org.springframework.web.servlet.service(FrameworkServlet.java:807) at javax.servlet.http.Servlet.service(HttpServlet.java.httpServlet.service.httpServlet.service.722) at org.apache..java core.lina.210) at org.apache.catalet.core.deFilterchain.doFilter(ApplicationFilterChain.java:210) atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.lina.core.StandardHostValve.invoke(标准主机.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) atorg.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.standardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.http11Processor.process(Http11处理器.java:250) at org.apache.coyote.http11.http11Protocol$http11.comecthandler.process(Http11Protocol.java:188) at org.apache.coyote.http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadExcutor$Worker.run(ThreadPoolExcutor.java:615) at java.lang.Thread.run(Thread.java:745)
此外,当我执行以下 URL 时,它会生成 HTTP 状态 405 - 请求方法“GET”不受支持
更改<code>SpringServiceController的请求方法。SpringServiceController方法从<code>POST。这应该能解决你的问题。
@RequestMapping(value="/insert/{user_id}/{question_id}/{answer}",method = RequestMethod.GET,headers="Accept=application/json")
我使用curl获取http头以查找http状态代码并返回响应。我使用以下命令获取http头
问题内容: 如果我返回一个对象: 状态代码将为200。如何将其更改为201,并显示一条消息并与json对象一起发送? 我不知道是否有一种方法可以在Laravel中设置状态代码。 问题答案: 您可以用来设置HTTP响应代码。 如果未传递任何参数,则http_response_code将获取当前状态码。如果您传递参数,它将设置响应代码。 对于Laravel:
我想知道在REST API中应该如何响应。 有效示例: 以上是一个有效的请求,当前我的HTTP状态为200,有一个JSON响应
我对Spring很陌生。我有一个在Spring中编写的REST api,但我不知道如何使用自定义http响应代码返回JSON响应。 我返回一个JSON响应,如下所示: 但它总是显示200个http ok状态代码。 如何可能在void函数中返回JSON响应和自定义HTTP代码?
好的,我在运行我的应用程序时遇到了这个问题,我相信这是因为版本控制。 秋季开始,我使用Tomcat 9.0.4。我使用最新的JavaSDK。 现在,我使用的是Spring版本5.0.2。 这是我的pom。xml: 这是我的web.xml: 这是我的调度器servlet。xml: 现在,我想提一件事:看看那些?我不确定这是否正确。为什么?这就是我在输出中得到的错误: 现在,我知道这是一大段文字,你可
我编写了以下控制器方法: 当我在浏览器中请求此方法时,我看到以下内容: 我实现服务器端错误还是客户端问题? 更新NJNEX答案。 如果我想在控制器方法中实现以下逻辑,我应该怎么做: