GSON对象(RESTful api)

时间:2017-04-05 10:33:01

标签: java c++ json rest gson

我有一个客户端(java)将一些json数据发送到我的服务器(c ++)。在此之后我的服务器响应一些信息,这取决于我的java客户端的操作。现在它的作品。

Example body request:
{
   "userEmail": "email@email.com",
   "userPassword": "12345678"
}

服务器,接收电子邮件和密码并执行操作并发回回复。

但现在我需要更改我的java客户端以发送这样的请求:

{
  "userInformation":{
        "userEmail": "email@email.com",
        "userPassword": "12345678"
  }

}

此请求可用于登录。这是一个非常复杂的架构,所以我无法复制所有代码,但在登录类中我使用了gson(注意我只在c ++服务器上工作,我不在客户端工作,不可能联系那个让java客户端问他的人关于这个疑惑)

Java客户端 - 登录类

public final String userEmail;
public final String userPassword;


public LoginRequestArgs( String userEmail, String userPassword)
{
    this.userEmail = userEmail;
    this.userPassword = userPassword;       
}

public static LoginRequestArgs fromStringJson(String data)
{
    try
    {
        Gson gson = new Gson();
        return gson.fromJson(data, LoginRequestArgs.class);
    }
    catch(Exception e)
    {
        return null;
    }
}

public static LoginRequestArgs fromBytesJson(byte[] data)
{
    if (data == null) return null;
    try 
    {
        String str = new String(data, "utf-8");
        return fromStringJson(str); 
    }
    catch (Exception e) 
    {
        return null;
    }
}


@Override
public String toJsonString()
{
    try
    {
        Gson gson = new Gson();
        return gson.toJson(this);
    }
    catch(Exception e)
    {
        return null;
    }
}

@Override
public byte[] toJsonBytes()
{
    try 
    {
        return this.toJsonString().getBytes("utf-8");
    }
    catch (Exception e)
    {
        return null;
    }
}

我有其他类连接到我的服务器并发送请求。此类调用login函数(请求是SerializableJSON变量)并将此请求发送到我的服务器request = LoginRequestArgs.fromStringJson(args.httpEntity);

(对于测试我使用一个其他客户端chrome扩展名)

我知道这可能有些难以理解但不容易解释所有事情。我试着解释一下这个问题。

真正的问题:我无法让java客户端发送内部的电子邮件和密码" userInformation"。有人可以帮帮我吗?感谢

编辑(其他身体申请示例):

{
    "userInformation": {
        "otherSection": {
            "key1": "value1",
            "key2": "value2"
        }
    }
}

EDIT 2(身份验证方法):

{
    "authenticationMethod": {
        "sessionId": {
            "email": "email@email.com",
            "password": "pass123"
        }
    },
    "userInformation": {
        "userId": "user",
        "userPassword": "user123"
    }
}

EDIT 3(TOKEN):

{
    "authenticationMethod": {
        "token": {
            "token": "HDI393UDDNDMAY4758SAD"
        }
    },
    "userInformation": {
        "userId": "user",
        "userPassword": "user123"
    }
}

编辑4:

{
  "sessionId":{
    "email":"email@email.com",
    "password":"dasdas"

  },
  "userInformation":{
        "userId": "userId1",
        "userPassword": "12345678"
  }

}

我已经将这个json发送到我的c ++服务器,它的工作和我的解码会话ID也正常工作,这对我来说不是问题。但我需要发送sessionId(或令牌),但内部" AuthenticationMethod",它只是我现在需要实现的东西。注意" userInformation"它的例子和例子可以是例如" bookInformation"," carInformation" ,根据请求类型,我发送内部具有不同键/值的不同数据..但是在所有请求中始终必须使用身份验证方法(会话ID或令牌)。

像我向你展示的那样,我实现了这个:

public class SessionId {

    public String email;
    public String password;

    public SessionId(String email, String password)
    {
        this.email = email;
        this.password = password;
    }

}

在我的类的构造中(可以是一个类,如LoginRequestArgs,登录cass)我称之为超级:

public UserInfo userInformation;
public SessionId sessionId;

public LoginRequestArgs(String email, String password,String userEmail, String userPassword)
    {   
        super(email,password);
        userInformation = new UserInfo(userEmail, userPassword);
    }


static class UserInfo {
        public String userId;
        public String userPassword;

        public UserInfo(String userEmail, String userPassword) {
            this.userId = userEmail;
            this.userPassword = userPassword;
        }
    }

所以现在,我只需要添加" authenticationMethod"在会话ID或令牌之前(我相信这样做的方式对两者都是一样的)

****编辑5 ********

login.java

public class LoginRequestArgs implements SerializableJSON  {

    public UserInfo userInformation;
    public AuthenticationMethod authenticationMethod;

    public LoginRequestArgs(String email, String password,String userId, String userPassword)
    {   
        AuthenticationMethod auth = new SessionId(email, password);
        setAuth(auth);
        userInformation = new UserInfo(userId, userPassword);
    }

    public void setAuth(AuthenticationMethod authenticationMethod){
            this.authenticationMethod = authenticationMethod;
    }

    static class UserInfo {
        public String userId;
        public String userPassword;

        public UserInfo(String userEmail, String userPassword) {
            this.userId = userEmail;
            this.userPassword = userPassword;
        }
    }

SessionId.java

public class SessionId extends AuthenticationMethod {

    Session sessionId;

     public SessionId(String email, String password)
     {
         this.sessionId = new Session(email,password);

     }

     static class Session{
         String email;
         String password;

         public Session(String email, String password)
         {
             this.email = email;
             this.password = password;
         }

     }
}

AuthenticationMethod.java

public class AuthenticationMethod {


}

1 个答案:

答案 0 :(得分:1)

您需要将LoginRequestArgs类更改为以下内容:

public class LoginRequestArgs {

    public UserInfo userInformation;

    public LoginRequestArgs(String userEmail, String userPassword) {
        userInformation = new UserInfo(userEmail, userPassword);
    }

    static class UserInfo {
        public String userEmail;
        public String userPassword;

        public UserInfo(String userEmail, String userPassword) {
            this.userEmail = userEmail;
            this.userPassword = userPassword;
        }
    }

    public static LoginRequestArgs fromStringJson(String data) {
        try {
            Gson gson = new Gson();
            return gson.fromJson(data, LoginRequestArgs.class);
        } catch (Exception e) {
            return null;
        }
    }

    public static LoginRequestArgs fromBytesJson(byte[] data) {
        if (data == null)
            return null;
        try {
            String str = new String(data, "utf-8");
            return fromStringJson(str);
        } catch (Exception e) {
            return null;
        }
    }

}

以下是访问电子邮件和密码的方法:

loginRequestArgsInstance.userInformation.userEmail;
loginRequestArgsInstance.userInformation.userPassword;

您可能应该在此课程中添加一些getter和setter,或者至少确保userInformation不是null

将会话详细信息包含在JSON中有多种方法。一种方法是修改你的java类。像这样:

Req.java

public class Req{
    String data1;
    String data2;
    Auth authenticationMethod;
    ....
    public void setAuth(Auth authenticationMethod){
        this.authenticationMethod = authenticationMethod;
    }
}

Auth.java

public class Auth{
   ....
}

AuthToken.java

public class AuthToken extends Auth {

    Token token;

    public AuthToken(String token) {
        this.token = new Token(token);
    }

    static class Token {
        String token;

        public Token(String token) {
            this.token = token;
        }

    }
}

AuthUserInfo.java

public class AuthUserInfo extends Auth {

    UserInfo sessionId;

    public AuthUserInfo(String email, String password) {
        this.sessionId = new UserInfo(email, password);
    }

    static class UserInfo {
        String email;
        String password;

        public UserInfo(String email, String password) {
            this.email = email;
            this.password = password;
        }
    }
}

以下是您可以使用这些课程的方法:

Req req = new Req(...);
Auth auth = new AuthToken(...);// OR: new AuthUserInfo(...);
req.setAuth(auth);
String json = new Gson().toJson(req);

另一种方法是在创建JSON后为其添加新属性。您可以看到如何执行此操作的示例here