1

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

enter image description here

Sample output

[
 {
 "sinvestigatorname": "Bargman",
 }
]

How to convert This output into JSON format(key,value) Can any one help me how to do that

asked Feb 13, 2018 at 9:49
4
  • I tried with selecting two column output not coming in json format sample output [ 65363, "Allis, C David" ], Commented Feb 13, 2018 at 11:45
  • which configuration Commented Feb 13, 2018 at 11:53
  • what i need to change Commented 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 here Commented Feb 13, 2018 at 11:59

1 Answer 1

2

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

answered Feb 13, 2018 at 17:19
Sign up to request clarification or add additional context in comments.

7 Comments

No. You can't. You should define a model and map it when using native query.
i tried like this: @Query(value="select invest.sinvestigatorname as sinvestigatorname," + "invest.ninvestigatorid as ninvestigatorid\r\n" + " from investigator as invest where invest.ninstid=60",nativeQuery=true) Set<Object[]> findBySinvestigatorname();
its result giving only value not key. what i need to change in this Query
You can use projections to map the results.
What are you trying to accomplish? Instead of coming up with new questions in comments, Please clearly state your problem, what have you tried to solve the problem and seek for help. I'd be happy to help.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.