本文介绍了Gson在转换时忽略我的领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个模型:
public class UserRequest extends DefaultRequest {
public String username;
公共字符串密码;
公共字符串ID;
public UserRequest(String username,String password){
this.username = username;
this.password =密码;
}
}
我称之为:
// code truncated
UserRequest userRequest = new UserRequest(username,password);
response = getRestClient()。sysInitApp(userRequest).execute();
//代码截断
然后打印出请求主体
$ p $
{
用户名:farid,
password:passfarid ,
id:null
}
我得到了:
{
用户名:farid,
密码:passfarid
}
我很感谢这个问题的任何帮助。
解决方案
来自 javadocs ...您可以使用 GsonBuilder
构造您的 Gson
实例,并选择opt ()
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
I created a model:
public class UserRequest extends DefaultRequest {
public String username;
public String password;
public String id;
public UserRequest(String username, String password) {
this.username = username;
this.password = password;
}
}
And I'm calling it like:
//code truncated
UserRequest userRequest = new UserRequest(username,password);
response = getRestClient().sysInitApp(userRequest).execute();
//code truncated
And then I print out request body, instead of:
{
"username":"farid",
"password":"passfarid",
"id":null
}
I get:
{
"username":"farid",
"password":"passfarid"
}
I would appreciate any help with this issue.
解决方案
from the GsonBuilder javadocs... you can use GsonBuilder
to construct your Gson
instance, and opt in to have null values serialized as so:
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
这篇关于Gson在转换时忽略我的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!