繁体   English   中英

基本REST Web服务无法注册

[英]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上打开了ApacheMySql并创建了表。 我还在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM