how to return a json result in springboot

Viewed 163

I am trying to return a json result with the result format of

{
     "code": "0158"
}

but I end up with the value

[
    "0158"
]

am not sure what am doing wrong, I have tried two approaches and each give me the same value.

Option 1 I used a controller and a service here is my implementation The controller

@GetMapping(path = "/eareport/spotchecksteams")
    public ResponseEntity<List> getSpotChecksteams(@RequestParam("country") String country,
                                                @RequestParam("projectId") String projectId){
        List<SpotCheckModel> report = dashboardService.getSpotChecksteams(country,projectId);
        return ResponseEntity.status(HttpStatus.OK).body(report);
    }

the service class

 public List<SpotCheckModel> getSpotChecksteams(String country, String projectId){

        TypedQuery<SpotCheckModel> query = (TypedQuery<SpotCheckModel>) entityManager.createQuery(
                "SELECT e.code FROM SpotCheckModel e WHERE e.country = :country AND e.project = :projectId");
        query.setParameter("country", country);
        query.setParameter("projectId", projectId);
        return query.getResultList();

    }

option 2 I used a controller and a repository my controller

@GetMapping(path = "/eareport/spotchecksteams")
    public Iterable<SpotCheckModel> getSpotChecksteams(@RequestParam("country") Optional<String> country,
                                                       @RequestParam("projectId") Optional<String> projectId){

        if(country.isPresent() && projectId.isPresent())
            return spotChecksRepository.findEntriesByUserId(country.get(), projectId.get());
        else
            return null;

    }

my repository

public interface SpotChecksRepository extends CrudRepository<SpotCheckModel, String> {

    @Query("SELECT e.eacode FROM SpotCheckModel e WHERE e.country = ?1 AND e.project = ?2")
    public Iterable<SpotCheckModel> findEntriesByUserId(@Param("country") String country, @Param("projectId") String projectId);

}

my entity class

@Entity
@Table(name = "SPOT_CHECK")
public class SpotCheckModel {

    @Id
    @Column(name="ID")
    private Long id;
    @Column(name="Country")
    private String country;
    @Column(name="Project")
    private String project;
    @Column(name="HtmlContent")
    private String htmlcontent;
    @Column(name="FileName")
    private String filename;
    @Column(name="Code")
    private String code;
    @Column(name="CreateDate")
    private DateTime createdate;
    @Column(name="UpdateDate")
    private DateTime updatedate;
2 Answers

You need to modify your controller as follows to get the desired output.Instead of returning the List, you need to return the custom object.

@GetMapping(path = "/eareport/spotchecksteams")
    public ResponseEntity<SpotCheckModel> getSpotChecksteams(@RequestParam("country") String country,
                                                @RequestParam("projectId") String projectId){
        SpotCheckModel report = dashboardService.getSpotChecksteams(country,projectId);
        return ResponseEntity.status(HttpStatus.OK).body(report);
    }

You may try this code

@GetMapping(path = "/eareport/spotchecksteams")
public ResponseEntity<?> getSpotChecksteams(@RequestParam("country") String country,
                                                    @RequestParam("projectId") String projectId){

List<SpotCheckModel> report = dashboardService.getSpotChecksteams(country,projectId);
JSONObject jObj = new JSONObject();
try {
        jObj.put("code", report);
    } catch (JSONException e) {
        e.printStackTrace();
    }
 return ResponseEntity.status(HttpStatus.OK).body(jObj.toString());
}

Result:

{
   "code": "0158"
}
Related