For converting Query result(Array) to JSON format,
I tried with @ResponseBody annotation and also with produces="application/json" but no changed in output format.
I also tried with ObjectMapper() but that output in string format not in key value.I removed my trial which not working.Now I added here my current code.
Here is my code snippet.
InvestigatorModel
package com.demo.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="investigator")
public class InvestigatorModel implements Serializable{
 private static final long serialVersionUID = 1L;
 @Id
 public Integer ninvestigatorid;
 public String sinvestigatorname;
 public Integer ninstid ;
 public String stitle;
 public Integer ntempinvestigatorid;
 public Integer nempid;
 //getter and setter
InvestigatorController
package com.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.demo.model.InvestigatorModel;
import com.demo.services.InvestigatorService;
@RestController
@RequestMapping("/SpaceStudy/SpaceAdmin")
public class InvestigatorController {
 @Autowired
 InvestigatorService investService;
 @CrossOrigin(origins="*") 
 @GetMapping("/AccountMaintenance/LoadInvestigator") 
 public List<InvestigatorModel> findInvestigatorName()
 {
 return investService.findInvestigatorName();
 }
}
InvestigatorService
package com.demo.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.model.InvestigatorModel;
import com.demo.repository.InvestigatorRepository;
@Service
public class InvestigatorService 
{
 @Autowired
 InvestigatorRepository investRepo;
 public List<InvestigatorModel> findInvestigatorName()
 {
 return investRepo.findBySinvestigatorname();
 }
}
InvestigatorRepository
package com.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.demo.model.InvestigatorModel;
@Repository
public interface InvestigatorRepository extends JpaRepository<InvestigatorModel, Integer>
{
 @Query("select invest.sinvestigatorname from InvestigatorModel as invest where invest.ninstid=60")
 List<InvestigatorModel> findBySinvestigatorname();
}
My output is like This
Sample output
[
 {
 "sinvestigatorname": "Bargman",
 }
]
How to convert This output into JSON format(key,value) Can any one help me how to do that
- 
 I tried with selecting two column output not coming in json format sample output [ 65363, "Allis, C David" ],SpringUser– SpringUser2018年02月13日 11:45:57 +00:00Commented Feb 13, 2018 at 11:45
- 
 which configurationSpringUser– SpringUser2018年02月13日 11:53:35 +00:00Commented Feb 13, 2018 at 11:53
- 
 what i need to changeSpringUser– SpringUser2018年02月13日 11:55:20 +00:00Commented Feb 13, 2018 at 11:55
- 
 previously i checked with @ResposeBody but no changed in output so i removed that now i am working with same code which i added hereSpringUser– SpringUser2018年02月13日 11:59:25 +00:00Commented Feb 13, 2018 at 11:59
1 Answer 1
As you are using JPA, simplest solution would be:
@Query("select new map(invest.sinvestigatorname as sinvestigatorname) from InvestigatorModel invest where invest.ninstid=60")
List<InvestigatorModel> findBySinvestigatorname();
This would give you the desired result.
See docs here
7 Comments
Explore related questions
See similar questions with these tags.