简体   繁体   中英

Trying to modify toString() method to get desired results

I have overridden the toString methods in my Employee class as shown below. This is returning name and EmployeeId simultaneously as one single string which I don't want because of the reasons explained below.

 @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;

    }

Since I am returning name and EmployeeId in the toString method, because of the following line :

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

I am getting the following JSON response :

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

However, I would like to get the following response with all the empIDs printing next to empID and all the empStatus printing next to empStatus as shown below :

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

How should I modify my toString() method in the Employee class so that I could achieve above JSON response?

Here is the code for my controller:

@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;
    }

I could not understand why overriding method when you can create a normal method and returns the same response as you want.

Alternative just use as following:

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

And in your updateemployee method use emp.addJoinString() instead of emp.toString() . The above code was just the sample to remove the error. Now when parsing use split with comma and use both values as you want.

For creating the response as desired by you can use other libraries like Google Gson, Jackson, or JSON

For example you use 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM