Java 基本REST Web服务无法注册

Java 基本REST Web服务无法注册,java,android,mysql,rest,Java,Android,Mysql,Rest,首先,我对创建web服务非常陌生。以前总有人帮我做过,但这次我需要自己来做。请容忍我,我还是不太明白 我遵循了教程(您可以在那里下载源代码,我几乎遵循了所有内容),并根据需要更改了一些变量,如: //Change these parameters according to your DB public class Constants { public static String dbClass = "com.mysql.jdbc.Driver"; private static S

首先,我对创建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的默认浏览器(RunAs->RunAtServer)上运行web。我只知道tomcat已经在本地服务器上运行了

按照教程,我在XAMPP上打开了ApacheMySql,并创建了表。我还在chrome浏览器上安装了高级rest客户端

我尝试使用以下URL进行注册:
http://localhost:3306/useraccount/register/doregister?username=admin&password=admin
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;
    }
公共类寄存器{
//HTTP获取方法
@得到
//路径:http://localhost//register/doregister
@路径(“/doregister”)
//生成JSON作为响应
@产生(MediaType.APPLICATION_JSON)
//查询参数是以下参数:http://localhost//register/doregister?name=pqrs&username=abc&password=xyz
公共字符串doLogin(@QueryParam(“name”)字符串名称,@QueryParam(“用户名”)字符串uname,@QueryParam(“密码”)字符串pwd){
字符串响应=”;
//System.out.println(“内部多洛金”+uname+”+pwd);
int retCode=registerUser(名称、uname、pwd);
如果(retCode==0){
response=Utitlity.constructJSON(“register”,true);
}else if(retCode==1){
response=Utitlity.constructJSON(“register”,false,“您已经注册”);
}else if(retCode==2){
response=Utitlity.constructJSON(“register”,false,“用户名和密码中不允许使用特殊字符”);
}else if(retCode==3){
response=Utitlity.constructJSON(“register”,false,“出错”);
}
返回响应;
}
专用int注册器(字符串名称、字符串uname、字符串pwd){
System.out.println(“内部检查凭证”);
int结果=3;
if(Utitlity.isNotNull(uname)和&Utitlity.isNotNull(pwd)){
试一试{
if(DBConnection.insertUser(名称、uname、pwd)){
System.out.println(“RegisterUSer if”);
结果=0;
}
}捕获(SQLException sqle){
System.out.println(“RegisterUSer-catch-sqle”);
//发生主键冲突时,表示用户已注册
if(sqle.getErrorCode()==1062){
结果=1;
} 
//在名称、用户名或密码中使用特殊字符时
else if(sqle.getErrorCode()==1064){
System.out.println(sqle.getErrorCode());
结果=2;
}
}
捕获(例外e){
//TODO自动生成的捕捉块
System.out.println(“内部检查”);
结果=3;
}
}否则{
System.out.println(“内部检查凭证”);
结果=3;
}
返回结果;
}
请帮帮我,我做了我能做的一切,现在我迷路了


非常感谢您抽出时间。

我刚刚想起我在本地主机Looonggg上做了一些事情,所以正确的url是
http://localhost:8080/useraccount/register/doregister?username=admin&password=admin

现在我可以成功注册了,但我仍然不明白这段代码的用途:
publicstaticstring dbUrl=“jdbc:mysql://localhost:3306/“+dbName;
(请参阅我的问题)


请为我解释一下这些事情。

<3306:它是MySQL接受数据库请求的端口。 8080:它是tomcat侦听Http请求的端口

守则: 公共静态字符串dbUrl=“jdbc:mysql://localhost:3306/“+dbName

正在告诉jdbc(java数据库连接)url连接到mysql,以便在后端数据库上执行插入、删除和读取操作。DBConnection使用此常量连接到mysql数据库

以下是可能对您有所帮助的JDBC指南:

查看您的代码,我建议: 1.使用Log4j或Logback而不是System out 2.返回状态代码500表示内部错误,400表示验证错误等。以下是您可以使用的状态代码列表:

localhost:3306
是MySql服务器的主机:端口,而不是Web服务。