2017-07-07 286 views
0

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

方法hcpselect_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_isINT类型,所有其他都是VARCHAR

enter image description here

你能帮助我吗?

+0

日志/控制台中的任何异常?您是否尝试跟踪/调试IDE中的代码? –

+0

@JozefChocholacek我没有任何例外。这个问题可能出现在'Connection conn = this.getDBMySQLCon();'中,因为如果我在这行后面打印任何东西,我都看不到它。 – zinon

+0

我检查了'mySQL'连接,它现在有警告。我不知道问题是什么...... – zinon

回答

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

谢谢!现在更正是'preparedStmt.setString(1,String.valueOf(hcp_name))',因为'hcp_name'在存储过程中被用作'string'。 – zinon