[英]Basic REST Web Service can not register
首先,我是創建Web服務的新手。 以前總是有人為我做過,但是這次我需要自己構建。 請多多包涵,我還是不太了解。
我遵循了本教程(您可以在此處下載源代碼,我遵循了幾乎所有內容)並根據需要更改了一些變量,例如:
//Change these parameters according to your DB
public class Constants {
public static String dbClass = "com.mysql.jdbc.Driver";
private static String dbName= "users";
public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
public static String dbUser = "root";
public static String dbPwd = "";
}
我試圖運行該項目,但它僅在eclipse的默認瀏覽器(以->在服務器上運行)上運行網絡。 我所知道的是tomcat已經在本地服務器上運行。
我按照本教程的要求在XAMPP上打開了Apache和MySql並創建了表。 我還在chrome瀏覽器上安裝了高級rest客戶端 。
我嘗試使用以下URL進行注冊: http://localhost:3306/useraccount/register/doregister?username=admin&password=admin
和Advanced rest客戶端上的GET方法,結果為200 OK,但未返回JSON(它應該返回一些JSON)。
當我在phpmyadmin上檢查數據庫時,用戶表仍然為空,因此注冊失敗。
這是注冊課程:
public class Register {
// HTTP Get Method
@GET
// Path: http://localhost/<appln-folder-name>/register/doregister
@Path("/doregister")
// Produces JSON as response
@Produces(MediaType.APPLICATION_JSON)
// Query parameters are parameters: http://localhost/<appln-folder-name>/register/doregister?name=pqrs&username=abc&password=xyz
public String doLogin(@QueryParam("name") String name, @QueryParam("username") String uname, @QueryParam("password") String pwd){
String response = "";
//System.out.println("Inside doLogin "+uname+" "+pwd);
int retCode = registerUser(name, uname, pwd);
if(retCode == 0){
response = Utitlity.constructJSON("register",true);
}else if(retCode == 1){
response = Utitlity.constructJSON("register",false, "You are already registered");
}else if(retCode == 2){
response = Utitlity.constructJSON("register",false, "Special Characters are not allowed in Username and Password");
}else if(retCode == 3){
response = Utitlity.constructJSON("register",false, "Error occured");
}
return response;
}
private int registerUser(String name, String uname, String pwd){
System.out.println("Inside checkCredentials");
int result = 3;
if(Utitlity.isNotNull(uname) && Utitlity.isNotNull(pwd)){
try {
if(DBConnection.insertUser(name, uname, pwd)){
System.out.println("RegisterUSer if");
result = 0;
}
} catch(SQLException sqle){
System.out.println("RegisterUSer catch sqle");
//When Primary key violation occurs that means user is already registered
if(sqle.getErrorCode() == 1062){
result = 1;
}
//When special characters are used in name,username or password
else if(sqle.getErrorCode() == 1064){
System.out.println(sqle.getErrorCode());
result = 2;
}
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Inside checkCredentials catch e ");
result = 3;
}
}else{
System.out.println("Inside checkCredentials else");
result = 3;
}
return result;
}
請幫助我,我已盡力而為,我現在迷路了。
非常感謝您的寶貴時間。
我只記得我之前在localhost loooongggg上做了一些操作,所以正確的URL是http://localhost:8080/useraccount/register/doregister?username=admin&password=admin
。
現在我可以成功注冊,但是我仍然不明白該代碼的用途: public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
(請看我的問題)
請為我解釋這些事情。
3306:這是MySQL的端口,它接受數據庫請求。 8080:它是tomcat的端口,用於偵聽Http請求。
代碼:public static String dbUrl =“ jdbc:mysql:// localhost:3306 /” + dbName;
告訴jdbc(Java數據庫連接性)URL連接到mysql,以便在后端數據庫上執行插入,刪除和讀取操作。 DBConnection利用此常量連接到MySQL數據庫。
這是一個可能對您有幫助的JDBC指南: http : //www.tutorialspoint.com/jdbc/jdbc-quick-guide.htm
查看您的代碼,我建議:1.使用Log4j或Logback代替System out。2.返回狀態碼500表示內部錯誤,返回400表示驗證錯誤等。這是可以使用的狀態碼列表: http:/ /www.restapitutorial.com/httpstatuscodes.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.