简体   繁体   English

Java WebServlet API:HTTP 400错误-错误的请求

[英]Java webservlet API: HTTP 400 error - bad request

I have an API in JAVA. 我在JAVA中有一个API。

This is the form where I call it: 这是我称之为的表格:

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

This is the code of my API webservlet. 这是我的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;
        }

    }
}

When it goes to JSONObject jObj = new select_data().hcp(hcp_name); 当转到JSONObject jObj = new select_data().hcp(hcp_name); I get http 400 error: Bad request . 我收到http 400 error: Bad request

Method hcp in select_data is the one below. select_data中的hcp方法是下面的方法。 In the code above I have import dbmodule.select_data; 在上面的代码中,我import dbmodule.select_data; in order to see it. 为了看到它。

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(?) is a stored procedure in a mysql database. CALL select_hcp(?)是mysql数据库中的存储过程。 When I run this procedure in phpmyadmin is working properly. 当我在phpmyadmin中运行此过程时,它可以正常工作。 The problem is in my java code. 问题出在我的Java代码中。 I double checked the input json string and is correct. 我仔细检查了输入的json字符串,它是正确的。

This is the table hcp in my database, where hcp_is is type INT and all the other are VARCHAR . 这是我数据库中的hcp表,其中hcp_isINT类型,其他所有都是VARCHAR

在此处输入图片说明

Can you help me please? 你能帮我吗?

In the select_hcp servlet, use e.printStackTrace() to get the error + stack trace into log. select_hcp servlet中,使用e.printStackTrace()将错误+堆栈跟踪记录到日志中。 Just System.out.println("error"); 只是System.out.println("error"); only reports there is a problem, but does not say anything about what and where the problem is. 只报告有问题,但并没有说什么其中的问题是什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM