gpt4 book ai didi

java - Gson 转换为 Java Pojo 使值 null

转载 作者:行者123 更新时间:2023-12-02 03:45:40 25 4
gpt4 key购买 nike

我必须使用 hibernate 将 POJO 类 Community 保存在数据库中。我在 ManageCommunity 类中有这个方法:

public void persistCommunity(String name,String domain,String idCustomer){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Community aCommunity=new Community();
aCommunity.setName(name);
aCommunity.setDomain(domain);
aCommunity.setIdCustomer(idCustomer);
// aCommunity.setIdCommunity(aCommunity.getIdCommunity());
session.save(aCommunity);
session.getTransaction().commit();
}

在带有 doPost 的 servlet 上,我调用此方法并传递由 postman 发送的 json 参数:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

Gson g = new Gson();
System.out.println(CommunityHome.getBody(req));
Community community = g.fromJson(CommunityHome.getBody(req), Community.class);
System.out.println("Community: "+community);

HttpSession session = req.getSession(true);
try {
ManageCommunity manageCommunity = new ManageCommunity();
manageCommunity.persistCommunity(community.getName(),community.getDomain(),community.getIdCustomer());

} catch (Exception e) {

e.printStackTrace();
}
}

现在community的值为null,但是如果我看到getBody(req),就会有json

这是 getBody:

private static String getBody(HttpServletRequest request) throws IOException {

String body = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;

try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} /*finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}*/

body = stringBuilder.toString();
return body;
}

这是 POJO 类:

public class Community{
public Community(){
UUID uuid=UUID.randomUUID();
String random=uuid.toString();
this.idCommunity=random;
}



public String getIdCommunity() {
return idCommunity;
}
public void setIdCommunity(String idCommunity) {
this.idCommunity = idCommunity;
}
public String getIdCustomer() {
return idCustomer;
}
public void setIdCustomer(String idCustomer) {
this.idCustomer = idCustomer;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

private String idCommunity;
private String idCustomer;
private String domain;
private String name;

}

CommunityHome.getBody(req)的内容是:

{"idCustomer":"bb05ee35-0494-49e3-9e40-38d5dae10668", "name":"VenditaAbiti2", "domain":"Commerce2"}

最佳答案

将你的 json 更改为:

{"IdCustomer":"bb05ee35-0494-49e3-9e40-38d5dae10668",
"Name":"VenditaAbiti2",
"Domain":"Commerce2"
}

让它在这个小例子中工作:

String json = "{\"IdCustomer\":\"bb05ee35-0494-49e3-9e40-38d5dae10668\",\"Name\":\"VenditaAbiti2\",\"Domain\":\"Commerce2\"}";
Gson g = new Gson();
Community community = g.fromJson(json, Community.class);
System.out.println("Community:" + community.toString());

}

class Community {
@SerializedName("IdCustomer")
private String idCustomer;

@SerializedName("Name")
private String name;

@SerializedName("Domain")
private String domain;

@Override
public String toString() {
return "Community [idCustomer=" + idCustomer + ", name=" + name + ", domain=" + domain + "]";
}

//getter setter

输出:社区:社区 [IdCustomer=bb05ee35-0494-49e3-9e40-38d5dae10668,Name=VenditaAbiti2,Domain=Commerce2]

<小时/>

编辑1:

您的 Pojo 变量名称应与 json 字符串的键名称匹配。表示 json 键 "IdCustomer" 以大写字母开头。您的变量以小写字母开头。这无法匹配。但是您可以像我的回答一样注释您的变量。您可以在此处给出json 键的显式名称

关于java - Gson 转换为 Java Pojo 使值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36335883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com