1

How can we return multiple entity classes as XML response in the spring rest service?

Example: I need a response like the following

<GetEmployeeById>
            <Response>
                <Status>0</Status>
                <Message>Data found</Message>
            </Response>
            <Data>
                <Employee>
                    <Id> 2 </Id>
                    <Name> Mani M </Name>
                    <Job> SOftware Developer </Job>
                    <Salary> 40000 </Salary>
                </Employee>
            </Data>         
        </GetEmployeeById>

Here, Response and Employee are separate entities. I am getting the following XML response with my code. The issue here is, I am getting response node inside response node and employee node inside employee node.

<GetEmployeeById>
   <Response>
      <Response>
         <Status>0</Status>
         <Message>Data found</Message>
      </Response>
   </Response>
   <Employee>
      <Employee>
         <Id>2</Id>
         <Name>Mani M</Name>
         <Job>SOftware Developer</Job>
         <Salary>12000</Salary>
      </Employee>
   </Employee>
</GetEmployeeById>

Following is the java class where I am combining both the entity classes.

@XmlRootElement (name="GetEmployeesById")
public class GetEmployeesById implements Serializable{

    
    private static final long serialVersionUID = 1L;
    
    private List<Employee> Employee = new ArrayList<Employee>();
    
    private List<Response> Response = new ArrayList<Response>();

    public List<Employee> getEmployee() {
        return Employee;
    }

    public void setEmployee(List<Employee> employee) {
        Employee = employee;
    }

    public List<Response> getResponse() {
        return Response;
    }

    public void setResponse(List<Response> Response) {
        Response = Response;
    }
    
    
}

Kindly help me with this.

Muthu
  • 87
  • 2
  • 9

2 Answers2

1

Your problem here is with List<CustomResponse> as it is a List, the parent </Response> tag represents the List and child </Response> represents the element in the List

In order to achieve your desired payload, Response cannot be a List and has to be CustomResponse.

As for your data node, you might want to try creating a new class as per below

@XmlRootElement(name = "Data")
public class Data implements Serializable {
   private static final long serialVersionUID = 1L;
   
   @XmlElement(name = "Employee")
   private List<Employee> employees
}

You can then replace List<Employee> with Data in your GetEmployeesById class

mkjh
  • 1,634
  • 13
  • 25
1

Rename field and getters

// private List<Employee> Employee = new ArrayList<Employee>();
private List<Employee> Data = new ArrayList<Employee>();

or add annotation @XmlElement(name="Data") to field or getter (not sure)

and change field (also getters)

// private List<Response> Response = new ArrayList<Response>();
private Response Response = new Response();
Zavael
  • 2,383
  • 1
  • 32
  • 44
  • Modified as your recommendation, now Data node is coming as both parent and child nodes. 2 Kumar M Junior Software Developer 12000 0 Data found – Muthu Jun 02 '21 at 10:22
  • https://stackoverflow.com/questions/19194567/how-to-get-xmlelement-name-in-list-of-xmlelements here are some hints, I would try `@XmlVariableNode("name")` but i am not able to verify it currently – Zavael Jun 02 '21 at 10:37