First, I am very new on creating a web service. Someone always did it for me before, but this time I need to build it on my own. Please bear with me, I still do not understand a lot.
I followed this tutorial (you can download the source code there, I followed almost everything) and changed some variable on my need, like :
//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 = "";
}
I tried to run the project, but its only running the web on eclipse's default browser (run as -> run at server). All I know is the tomcat is already running on the local server .
I followed the tutorial by turning on Apache and MySql on my XAMPP and created the table. I also installed Advanced rest client on chrome browser.
I tried to do the registration by using this URL : http://localhost:3306/useraccount/register/doregister?username=admin&password=admin
and GET method on Advanced rest client, and the result is 200 OK but no JSON was returned (it should be return some JSON).
When I checked my DB on phpmyadmin, the user table is still empty so the registration is failed.
This is the registration class :
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;
}
Please help me out, I did whatever I can and I am lost now.
Thanks a lot for your time.
I just remembered I did something with the localhost loooongggg ago, so the correct url is http://localhost:8080/useraccount/register/doregister?username=admin&password=admin
.
Now I can register successfully, but I still do not understand what is this code for : public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
(please see my question)
Please kindly explain those things for me.
3306: It is the port for MySQL which accepts database requests. 8080: It is the port for tomcat which listens to Http requests.
The code: public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
is telling the jdbc (java database connectivity) url to connect to mysql for doing insert, delete , read operations on your backend database. The DBConnection utilizes this constant to connect to the MySQL database.
Here is a JDBC guide that may be helpful to you: http://www.tutorialspoint.com/jdbc/jdbc-quick-guide.htm
Looking at your code, I would recommend: 1. Use Log4j or Logback instead of System out 2. Return status code 500 for internal erros, 400 for validation errors etc. Here is a list of status codes that you can utilize: http://www.restapitutorial.com/httpstatuscodes.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.