java 如何在 Spring MVC 和 Hibernate 中将对象列表转换为 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32449476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:13:13  来源:igfitidea点击:

How to convert list of Objects to JSON in Spring MVC and Hibernate?

javajsonspringhibernatespring-mvc

提问by Nikhil Bharadwaj

I'm Using Spring MVC and hibernate to fetch data from MySQL. In the controller class a ModelAndView method listEmployees is returning a MAP.The method is getting list of Employee objects from EmployeeService class. I want this method to take list of Employee Objects from Service class and covert it into JSON,or convert it into JSON and write that data in a JSON file so that I can use this JSON data in a JSP page.

我正在使用 Spring MVC 和 hibernate 从 MySQL 获取数据。在控制器类中,ModelAndView 方法 listEmployees 返回一个 MAP。该方法从 EmployeeService 类获取 Employee 对象列表。我希望此方法从服务类中获取员工对象列表并将其转换为 JSON,或者将其转换为 JSON 并将该数据写入 JSON 文件中,以便我可以在 JSP 页面中使用此 JSON 数据。

@RequestMapping(value="/employees", method = RequestMethod.GET)  
 public @ResponseBody ModelAndView listEmployees() {  
  Map<String, Object> model = new HashMap<String, Object>();
  model.put("employees",prepareListofBean(employeeService.listEmployeess()));  
  return new ModelAndView("employeesList", model);  

 }

private List<EmployeeBean> prepareListofBean(List<Employee> employees){
        List<EmployeeBean> beans = null;
        if(employees != null && !employees.isEmpty()){
            beans = new ArrayList<EmployeeBean>();
            EmployeeBean bean = null;
            for(Employee employee : employees){
                bean = new EmployeeBean();
                bean.setName(employee.getEmpName());
                bean.setId(employee.getEmpId());
                bean.setAddress(employee.getEmpAddress());
                bean.setSalary(employee.getSalary());
                bean.setAge(employee.getEmpAge());
                beans.add(bean);
            }
        }
        return beans;
    }

EDIT

编辑

In this below method I tried GSON to convert list to JSON,but was getting null pointer Exception.

在下面的这个方法中,我尝试 GSON 将列表转换为 JSON,但得到空指针异常。

**@RequestMapping(value="/employees1", method = RequestMethod.GET)  
    public ModelAndView employeelist() {  

    Gson gson=new Gson();
    gson.toJson(employeeService.listEmployeess());

    ModelAndView modelAndView=new ModelAndView();
    modelAndView.addObject(gson);

    return modelAndView;

    }**

What could be the mistake in this above method?

上述方法可能有什么错误?

采纳答案by Naman Gala

You need to assign returned json value to Stringobject and then pass it to ModelAndView. Refer Gson.toJson()and also make sure your class(EmployeeBean) is serializable.

您需要将返回的 json 值分配给Stringobject,然后将其传递给ModelAndView. 参考Gson.toJson()并确保您的 class( EmployeeBean) 是serializable.

@RequestMapping(value = "/employees1", method = RequestMethod.GET)
public ModelAndView employeelist() {
    Gson gson = new Gson();
    String json = gson.toJson(employeeService.listEmployeess());

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject(json);

    return modelAndView;
}