GSON Object (RESTful api)

GSON Object (RESTful api)

本文关键字:api RESTful Object GSON      更新时间:2023-10-16

我有一个客户端(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;
}
}

我有其他类连接到我的服务器并发送请求。此类调用登录函数(请求是一个可序列化的 JSON 变量),并将此请求下达到我的服务器request = LoginRequestArgs.fromStringJson(args.httpEntity);

(对于测试,我使用一个 REST 客户端 Chrome 扩展程序)

我知道这可能很难理解,但要解释所有事情并不容易。我试图解释一下。

真正的问题:我无法调整Java客户端来发送"userInformation"中的电子邮件和密码。有人可以帮我吗?谢谢

编辑(其他正文请求示例):

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

编辑 2(身份验证方法):

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

编辑 3(令牌):

{
"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 也可以工作,这对我在 c++ 上的工作来说不是问题。但是我需要发送sessionId(或令牌),但在"身份验证方法"中,它只是我现在需要实现的东西。请注意"用户信息"它的喜欢和示例,例如"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 这样的类,登录卡斯),我称之为 super:

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;
}
}

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

编辑 5 ********

登录.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;
}
}

会话 ID.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;
}
}
}

身份验证方法.java

public class AuthenticationMethod {

}

您需要将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类。像这样:

要求.java

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

身份验证.java

public class Auth{
....
}

身份验证令牌.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;
}
}
}

身份验证用户信息.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 后向其添加新属性。您可以在此处查看如何执行此操作的示例