Java Spring MVC@ResponseBody返回列表不是使用hibernate的正确json响应

Java Spring MVC@ResponseBody返回列表不是使用hibernate的正确json响应,java,json,spring,hibernate,spring-mvc,Java,Json,Spring,Hibernate,Spring Mvc,Spring MVC@ResponseBody返回列表在使用hibernate时不是正确的json响应(没有键和括号),但在不使用hibernate时得到正确的json响应 DAO public List getStudentData(){ String hql= "select s.id, s.name, s.email from Student s"; Query query= sessionFactory.getCurrentSession().createQuery(hql); Lis

Spring MVC@ResponseBody返回列表在使用hibernate时不是正确的json响应(没有键和括号),但在不使用hibernate时得到正确的json响应

DAO

public List getStudentData(){
String hql= "select s.id, s.name, s.email from Student s";
Query query=  sessionFactory.getCurrentSession().createQuery(hql);
List list= query.list();
return list;
}
控制器

@RequestMapping(value="/fetchAllData" , method=RequestMethod.GET)
public @ResponseBody List studentContainer1(HttpServletRequest response){
    return ss.getStudentData();
}
JSON(我得到了)

但我需要如下回复:

[{"id":1,"name":"Pratyush","email":"pratyush.ankit@gmail.com"}]

您应该将DAO功能更改为:

public List<Student> getStudentData(){
    String hql = "from Student";
    Query query =  sessionFactory.getCurrentSession().createQuery(hql);
    List<Student> list = query.list();
    return list;
}
公共列表getStudentData(){
字符串hql=“来自学生”;
Query Query=sessionFactory.getCurrentSession().createQuery(hql);
List=query.List();
退货清单;
}

按照您定义hql查询的方式,它将以[id、名称、电子邮件]的形式返回数组的
List
。以后将无法将其转换为正确的json(此时标签将丢失)。您要做的是使
getStudentData()
返回
List
Student
对象。

您应该将DAO函数更改为:

public List<Student> getStudentData(){
    String hql = "from Student";
    Query query =  sessionFactory.getCurrentSession().createQuery(hql);
    List<Student> list = query.list();
    return list;
}
公共列表getStudentData(){
字符串hql=“来自学生”;
Query Query=sessionFactory.getCurrentSession().createQuery(hql);
List=query.List();
退货清单;
}
按照您定义hql查询的方式,它将以[id、名称、电子邮件]的形式返回数组的
List
。以后将无法将其转换为正确的json(此时标签将丢失)。您想要的是使
getStudentData()
返回
List
Student
对象。

您还(1)应该使用泛型,而不是原始类型,以及(2)使用Spring数据JPA为您自动生成DAO。您还(1)应该使用泛型,而不是原始类型,以及(2)使用Spring数据JPA为您自动生成DAO。