我正在编写一个基于HTTP的RESTful API SMP,我需要编写一个API,该API查找具有与输入相同的句柄的用户,并返回所有用户信息,因此返回整行。但是现在它什么也没返回。我找不到问题,可能有什么建议吗?谢谢您的帮助!
/api/seeuser // find a user and give information
// Input: curl -d '{"handle":"@cooldude42", "password":"mysecret!"}' -H "Content-Type: application/json" -X POST http://localhost:9990/api/seeuser/2 (Links to an external site.)
// 2 = Identity.idnum
// Output: {"status":"1", "handle":"@carlos", "fullname":"Carlos Mize", "location":"Kentucky", "email":[email protected]", "bdate":"1970-01-26","joined":"2020-04-01"}
// Output: {}. // no match found, could be blocked, user doesn't know. [EDIT: 04/14]
// etc.
API.java
@GET
@Path("/seeuser/{idnum}")
@Produces(MediaType.APPLICATION_JSON)
public Response seeuser(@PathParam("idnum") String idnum) {
String responseString = "{\"status_code\":0}";
StringBuilder crunchifyBuilder = new StringBuilder();
try {
String jsonString = crunchifyBuilder.toString();
Map<String, String> myMap = gson.fromJson(jsonString, mapType);
String handle = myMap.get("handle");
Map<String,String> teamMap = Launcher.dbEngine.seeuser(idnum, handle);
responseString = Launcher.gson.toJson(teamMap);
} catch (Exception ex) {
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
ex.printStackTrace();
return Response.status(500).entity(exceptionAsString).build();
}
return Response.ok(responseString)
.header("Access-Control-Allow-Origin", "*").build();
}
DBEngine.java
public Map<String,String> seeuser(String idnum, String handle) {
Map<String,String> userIdMap = new HashMap<>();
PreparedStatement stmt = null;
Integer id = Integer.parseInt(idnum);
try
{
Connection conn = ds.getConnection();
String queryString = null;
queryString = "SELECT * FROM Identity WHERE handle = ?";
stmt = conn.prepareStatement(queryString);
stmt.setInt(1,id);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
handle = rs.getString("handle");
String pass = rs.getString("pass");
String fullname = rs.getString("fullname");
String location = rs.getString("location");
String email = rs.getString("email");
String bdate = rs.getString("bdate");
userIdMap.put("handle", handle);
userIdMap.put("pass", pass);
userIdMap.put("fullname", fullname);
userIdMap.put("location", location);
userIdMap.put("email", email);
userIdMap.put("bdate", bdate);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return userIdMap;
}
我假设将把句柄作为null或空传递给Launcher.dbEngine.seeuser(idnum,handle)方法。