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;