gpt4 book ai didi

Java webservlet API : HTTP 400 error - bad request

转载 作者:行者123 更新时间:2023-12-02 12:40:13 26 4
gpt4 key购买 nike

我有一个 JAVA API。

这是我称之为的形式:

<form action="select_hcp" method="POST">
<div>
<textarea rows="4" cols="100" name="data"></textarea>
</div>
<div>
<input type="submit" class="btn btn-info" value="select_hcp" />
</div>
</form>

这是我的 API webservlet 的代码。

@WebServlet("/select_hcp")

public class select_hcp extends CEFCYServlet {
private static final long serialVersionUID = 1L;

public select_hcp() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HashMap<String, String> jsonData;
// If there was a problem parsing the JSON data
try {
if ((jsonData = parseSTJSON(getJSONString(request), response)) == null) {
return;
}
// Get data from json
String hcp_name = jsonData.get("hcp_name");
System.out.println(hcp_name);
// insert data to db
JSONObject jObj = new select_data().hcp(hcp_name);
// Auditing
// Set the success message with the results
setSuccessMessage(response, jObj);
} catch (Exception e) {
System.out.println("error");
setErrorMessage(response, 400, "Bad Request", "Could not select data. Check the data given as input.");
return;
}

}
}

当它转到 JSONObject jObj = new select_data().hcp(hcp_name); 时,我收到 http 400 错误:错误请求

select_data 中的 hcp 方法如下。在上面的代码中,我使用 import dbmodule.select_data; 来查看它。

public JSONObject hcp(String hcp_name) {
JSONObject obj = null;
Connection conn = this.getDBMySQLCon();
ResultSet rs = null;
String queryString = "{CALL select_hcp(?)}";
PreparedStatement preparedStmt = null;
try {
preparedStmt = conn.prepareCall(queryString);
preparedStmt.setInt(1, Integer.valueOf(hcp_name));

boolean results = preparedStmt.execute();
int rowsAffected = 0;
// Protects against lack of SET NOCOUNT in stored procedure
while (results || rowsAffected != -1) {
if (results) {
rs = preparedStmt.getResultSet();
break;
} else {
rowsAffected = preparedStmt.getUpdateCount();
}
results = preparedStmt.getMoreResults();
}
int i = 0;
obj = new JSONObject();
while (rs.next()) {
JSONObject nested_obj = new JSONObject();
nested_obj.put("hp_name_or_legal_org", rs.getString("hp_name_or_legal_org"));
nested_obj.put("telephone_no", rs.getString("telephone_no"));
nested_obj.put("email", rs.getString("email"));
nested_obj.put("hcp_id", rs.getString("hcp_id"));
obj.put("hcp" + i, nested_obj);
i++;
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return (obj != null) ? obj : null;
}

CALL select_hcp(?)是mysql数据库中的存储过程。当我在 phpmyadmin 中运行此程序时工作正常。问题出在我的java代码中。我仔细检查了输入的 json 字符串并且是正确的。

这是我的数据库中的表hcp,其中hcp_is是类型INT,所有其他都是VARCHAR >.

enter image description here

你能帮我吗?

最佳答案

select_hcp servlet 中,使用 e.printStackTrace() 将错误 + 堆栈跟踪获取到日志中。只是 System.out.println("error"); 仅报告存在问题,但没有说明内容位置问题是。

  // ...
} catch (Exception e) {
e.printStackTrace(); // instead of System.out.println("error");
setErrorMessage(response, 400, "Bad Request", "Could not select data. Check the data given as input.");
return;
}

关于Java webservlet API : HTTP 400 error - bad request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44966949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com