将List <object>转换为JSON

时间:2018-12-06 15:03:15

标签: spring-data-jpa jpql

我正在使用spring data jpa从数据库中获取数据。我在存储库接口中为此编写了代码。但是我以以下格式获取jpql输出:

[[21632,"Allfrey Laboratory","110060"],[21633,"Allis Laboratory","110070"]

但是我希望此输出采用json格式,例如:

[{"ndept_id":21632,"sdept_name":"Allfrey Laboratory","ninst_id":60,"bis_locked":false,"sclient_dept_id":"110060","nsurvey_method_id":1,"bis_jointuse":false,"ntemp_dept_id":4,"balternate_jointuse_percentage":false,"ndiv_id":null}]

部门资料库

@Repository
public interface DepartmentsRepository extends JpaRepository<Department, Integer> 
{
    @Query("select d.ndept_id,d.sdept_name,d.sclient_dept_id from Department d")
    public List<Department> findColumns();
}

DepartmentController

 @RequestMapping(value="/findcolumn", method=RequestMethod.GET)
    @ResponseBody
    public List <Department> findColumnData()
    {
        return departmentservice.getColumns();
    }

1 个答案:

答案 0 :(得分:0)

我正在使用spring数据jpa来获取数据库数据。为此,我在存储库接口中使用了jpa查询。通过使用上述查询,我​​无法以正确的格式获取输出。我希望该输出为json。 为此,我在选择陈述中添加了new map。我解决了我的错误。

更新

@Repository
    public interface DepartmentsRepository extends JpaRepository<Department, Integer> 
    {
        @Query("select new map(d.ndept_id,d.sdept_name,d.sclient_dept_id) from Department d")
        public List<Department> findColumns();
    }