[英]Using Jackson to produce desired JSON response
誰能告訴我在使用Jackson打印JSON時我在做什么錯。 這是我的控制器代碼:
@RequestMapping(value="/get_employee_details", method=RequestMethod.GET)
public String updateemployee
(
@RequestParam(value="emp_id", defaultValue="0") Integer emp_id,
@RequestParam(value="name", defaultValue="") String name
)
{
String responseJSON = null;
boolean getStatus = true;
try {
EmployeeDao employeeDao = (EmployeeDao)context.getBean("employeeDao");
Employee employee = null;
List<Employee> empList = employeeDao.findByEmployeeId(emp_id);
if ((empList != null) && (!empList.isEmpty())) {
List<String> empStatus = new ArrayList<String>();
for(Employee emp : empList){
empStatus.add(emp.addJoinString());
responseJSON = GenericOrmStatusView.OrmResponseToJsonString(true, 1,empStatus, true);
}
}
}
return responseJSON;
}
我在Employee類中定義了以下方法:
public String addJoinString() {
return String.format("ID: %d",Name: %s," ,this.EmployeeId,this.name);
}
由於我在這里的代碼中運行一個for循環,並將列表empStatus
發送到OrmResponseToJsonString
方法:
for(Employee emp : empList){
empStatus.add(emp.addJoinString());
responseJSON = GenericOrmStatusView.OrmResponseToJsonString(true, 1,empStatus, true);
我收到以下JSON響應:
{
"status" : "SUCCESS",
"employeeStatus" : [ "ID: 81, Name: Jack", "ID: 83, Name: Anthony", "ID: 88, Name: Stephanie", "ID: 25, Name: Kelly", "ID: 02, Name: Jessica" ]
}
但是,我希望采用以下格式:
{
"status" : "SUCCESS",
"message": " "
},
"employeeStatus":[{
"ID":81,
"Name":"Jack"
},
{
"ID":88,
"Name":"Anthony"
},
and so on and so forth ....
]
以供參考:
我的OrmResponseToJsonString
方法在GenericOrmStatusView
類中定義如下
public class GenericOrmStatusView extends Views
{
public static String OrmResponseToJsonString(boolean success, List<String> eStatus,boolean pretty)
{
PrintemployeeStatusIDAndStatus statusMsg = WebServiceUtils.printNameAndID(success, eStatus);
String genericOrmStatusJsonString = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, pretty);
genericOrmStatusJsonString = objectMapper.writerWithView(Views.Normal.class).writeValueAsString(statusMsg);
//genericOrmStatusJsonString = objectMapper.writerWithView(Views.Normal.class).writeValueAsString(eStatus);
} catch(Exception e) {
e.printStackTrace();
}
return genericOrmStatusJsonString;
}
}
我的printNameAndID
方法在WebServiceUtils
類中定義如下:
public class WebServiceUtils
{
public static PrintNameAndID printNameAndID(boolean success, List<String> eStatus)
{
PrintNameAndID statusMsgAndRegID = new PrintNameAndID();
if (success) {
statusMsgAndRegID.setStatus("SUCCESS");
statusMsgAndRegID.setemployeeStatus(eStatus);
} else {
statusMsgAndRegID.setStatus("ERROR");
//statusMsgAndRegID.setemployeeStatus("");
}
return statusMsgAndRegID;
}
}
獲得所需JSON輸出的最簡單方法是創建一個表示數據的對象模型,例如
public class MyJSON {
private MyStatus status;
private List<EmployeeStatus> employeeStatus = new ArrayList<>();
public MyJSON(String status, String message) {
this.status = new MyStatus(status, message);
}
public void addEmployeeStatus(int id, String name) {
this.employeeStatus.add(new EmployeeStatus(id, name));
}
public MyStatus getStatus() {
return this.status;
}
public List<EmployeeStatus> getEmployeeStatus() {
return this.employeeStatus;
}
}
public class MyStatus {
private String status;
private String message;
public MyStatus(String status, String message) {
this.status = status;
this.message = message;
}
public String getStatus() {
return this.status;
}
public String getMessage() {
return this.message;
}
}
public class EmployeeStatus {
private int id;
private String name;
public EmployeeStatus(int id, String name) {
this.id = id;
this.name = name;
}
@JsonProperty("ID")
public int getId() {
return this.id;
}
@JsonProperty("Name")
public String getName() {
return this.name;
}
}
然后,您可以像這樣創建JSON文本:
MyJSON myJSON = new MyJSON("SUCCESS", " ");
myJSON.addEmployeeStatus(81, "Jack");
myJSON.addEmployeeStatus(88, "Anthony");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String json = objectMapper.writeValueAsString(myJSON);
System.out.println(json);
輸出量
{
"status" : {
"status" : "SUCCESS",
"message" : " "
},
"employeeStatus" : [ {
"ID" : 81,
"Name" : "Jack"
}, {
"ID" : 88,
"Name" : "Anthony"
} ]
}
就像chrylis在評論中提到的那樣 :
需要注意的是,通常可以讓Spring自動執行此操作,而只需從控制器方法
return empList
。
對於上面的代碼,其含義如下:
@GetMapping(path="/foo", produces="application/json")
public MyJSON foo() {
MyJSON myJSON = new MyJSON("SUCCESS", " ");
myJSON.addEmployeeStatus(81, "Jack");
myJSON.addEmployeeStatus(88, "Anthony");
return myJSON;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.