尝试修改toString()方法以获得所需的结果

时间:2018-07-25 14:52:55

标签: java spring-mvc

我在toString类中重写了Employee方法,如下所示。由于以下原因,我不希望将nameEmployeeId作为一个字符串同时返回。

 @Entity
    public class Employee 
    {       
        public int getEmployeeId() {
            return EmployeeId;
        }

        public void setEmployeeId(int EmployeeId) {
            this.EmployeeId = EmployeeId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Name:"+this.name+ "ID"+this.EmployeeId;
        }


        @Id
        @Column(name="Employee_ID")
        private int EmployeeId; 

        @Column(name="NAME")
        private String name;

    }

由于以下行,由于我要在toString方法中返回nameEmployeeId,所以:

responseJSON = GenericOrmStatusView.OrmStatusToJsonString(true, 1,empStatus, true);

我收到以下JSON响应:

{
  "status" : "SUCCESS",
  "empID" : 1,
  "empStatus" : [ "Name:JackID12", "Name:JohnID40", "Name:AndrewID27", "Name:TimothyID50", "Name:NikkiID65" ]
}

但是,我想得到以下响应,所有empID打印在empID旁边,所有empStatus打印在empStatus旁边,如下所示:

{
  "status" : "SUCCESS",
  "empID" : ["12","40","27","50","65"],
  "empStatus" : [ "Jack", "John", "Andrew", "Timothy", "Nikki" ]
}

如何修改toString()类中的Employee方法,以实现上述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.toString());
                responseJSON = GenericOrmStatusView.OrmStatusToJsonString(true, 1,empStatus, true);
            }

            }                       
        }

        return responseJSON;
    }

1 个答案:

答案 0 :(得分:0)

当您可以创建普通方法并返回所需的相同响应时,我不明白为什么要覆盖方法。

替代方法如下:

public String addJoinString() {
  return String.format("Name: %d, ID: %d",this.name,this.EmployeeId); 
}

在您的updateemployee方法中,使用emp.addJoinString()代替emp.toString()。上面的代码只是消除错误的示例。现在,在解析时,请使用comma拆分并根据需要使用两个值。

要创建所需的响应,可以使用其他库,例如Google Gson,Jackson或JSON

例如,您使用https://mvnrepository.com/artifact/org.json/json

if ((empList != null) && (!empList.isEmpty())) {

   JSONObject obj = new JSONObject();
   obj.put("status","success");
   obj.put("name",new JSONArray());
   obj.put("id",new JSONArray());
   for(Employee emp : empList){
     obj.get("name") .put(emp.name);
     obj.get("id") .put(emp.id);
     responseJSON = obj;
    }
}