[英]how to return a json result in springboot
I am trying to return a json result with the result format of我正在尝试返回 json 结果格式为
{
"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选项 1 我使用了 controller,这里的服务是我的实现 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服务 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选项 2 我使用了 controller 和存储库我的 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我的实体 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;
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.您需要按如下方式修改您的 controller 以获得所需的 output。您需要返回自定义 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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.