REST Service | JSON response format -
I have created a RAST web service using the Joint Replacement Imprint JX-RS. I have an employee POJO class which is sent as a response. The problem is in the response json format that i am returning to the list of employees back to the caller. Below is the code.
Below is the employee POOO class
@XmlAccessorType (XmlAccessType.FIELD) @XmlType (name = "", propOrder = {"employeeId", "name", "role "}) @ XmlRootElement (name =" employee ") public class employee {@XmlElement (required = true) protected string employee ID; @ XmlElement (required = true) name of protected name; @ XmlElement (required = true) protected string role; / ** * Employee ID gets the value of the property * * @ Return * Possible object * {@link String} * * / public string getEmployeeId () {return Employee Id; } / ** * Employee ID sets the value of the property * * @ absolute value * permission object * {@link string} * * / public zeros set id id (string value) {this.employeeId = value; } / ** * Name gets the value of property * * @ Return * is possible object * {@link name} * * / public name getName () {return name; } / ** * Name determines property value * * @ absolute value * permission item * {@link name} * * / public zeros setname (name value) {this.name = value; } / ** * Role gets value of property * * @ return * Possible object * {@link String} * * / public string getRole () {return role is; } / ** * Role sets the property value * * @ Ultimate Value * Permission Object * {@link string} * * / Public Zero Centrol (string value) {this.role = value; }} Below is a service method that sends a response.
@GET @ path ("/ json / employees /") @ product ("application / JS") public list & amp; Staff; & Gt; ListEmployeesJSON () {New ArrayList & lt; Employee & gt; (Employees.values ()); The response format generated is as follows: [{"employee": {"employee id": 876, "name": {"greeting": " "Employee", "first name": "human", "last name": "bnot"}, "role": "senior engineer"}}, {"employee": {"employee id": 923, "name": though However, the format of "mister", "first name": "benign", "last name": "mittal"}, "role": "senior engineer"}}] I want to be as:
{"employee": [{"employee id": 87 6, "name": {"greeting": "mister", "first name": "human", "last name": "bnot"}, "role": "senior engineer"}, {"employee id": 923, "Name": {"greeting": "mister", "first name": "benign
You can complete this by using a wrapper class. A simple example is:
Public Sector Employee Wager {Private Final Aralist & lt; Employee & gt; Staff; @ Jason Creator Public Employee WAPER (@Jason Rroprti ( "Staff") Arerelist & lt; Employee & gt; Employee) {This employee / staff; } Public Arrestist & lt; Employee & gt; GetEmployees () {Return employee; }} Return your wrapper object in response instead of the plain array list .
Comments
Post a Comment