我在JAVA中有一個API。Java webservlet API:HTTP 400錯誤 - 錯誤請求
這就是我稱之爲形式:
<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 error: Bad request
。
方法hcp
在select_data
是下面的一個。在上面的代碼中,我有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
。
你能幫助我嗎?
日誌/控制檯中的任何異常?您是否嘗試跟蹤/調試IDE中的代碼? –
@JozefChocholacek我沒有任何例外。這個問題可能出現在'Connection conn = this.getDBMySQLCon();'中,因爲如果我在這行後面打印任何東西,我都看不到它。 – zinon
我檢查了'mySQL'連接,它現在有警告。我不知道問題是什麼...... – zinon