【问题标题】:Java webservlet API: HTTP 400 error - bad requestJava webservlet API:HTTP 400 错误 - 错误请求
【发布时间】:2017-12-11 12:37:28
【问题描述】:

我有一个 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 error: Bad request

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_isINT 类型,其他都是VARCHAR

你能帮帮我吗?

【问题讨论】:

  • 日志/控制台中是否有异常?您是否尝试在 IDE 中跟踪/调试代码?
  • @JozefChocholacek 我没有任何例外。问题可能出在Connection conn = this.getDBMySQLCon();,因为如果我在此行之后打印任何内容,我将看不到它。
  • 我检查了mySQL 连接,它现在有警告。不知道是什么问题...
  • select_hcp servlet 中,将System.out.println("error"); 替换为e.printStackTrace(),然后查看日志。
  • @JozefChocholacek 我收到此错误:java.lang.NumberFormatException: For input string: "mak" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.valueOf(Unknown Source) 其中mak 是我用于搜索的关键字。

标签: java html api tomcat7


【解决方案1】:

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

【讨论】:

  • 谢谢!现在更正为preparedStmt.setString(1, String.valueOf(hcp_name)),因为hcp_name 在存储过程中用作string
猜你喜欢
  • 2021-12-03
  • 2015-10-07
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
相关资源
最近更新 更多