user10651355
user10651355

Reputation:

Convert List<object> to JSON

I am using spring data jpa to fetch data from database.I written code for that in repository interface .But I am getting jpql output in below format:

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

But i want this output in json format like:

[{"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}]

DepartmentRespository

@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();
    }

Upvotes: 1

Views: 363

Answers (1)

user10651355
user10651355

Reputation:

I am using spring data jpa to fetch database data .For that i used jpa query in the repository interface .By using above query i am not able to get fetched output in proper format.I want that output in json. For that i added new map in select statment. And i resolved my error.

Update:

@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();
    }

Upvotes: 1

Related Questions