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
