【发布时间】:2016-04-01 15:04:26
【问题描述】:
我必须使用休眠在数据库中持久化 POJO 类社区。我在类 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上,我调用了这个方法,并传递了邮递员发送的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();
}
}
现在社区的值为空,但如果我看到 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 不正确。现在,根级别不是您想要的社区。span>
-
我已经在线验证它是正确的..你的错误在哪里?
-
你的问题不清楚,请让你的帖子更清楚
-
@FilomenaDelSorbo 语法是正确的,但根级别是错误的。这意味着您的 JSON 需要一个包含 Community 字段的对象,而不仅仅是 idCostumer 等。
-
@FilomenaDelSorbo 你得到了什么 System.out.println(CommunityHome.getBody(req));