0

With spring i create a this webservice

@POST
    @Path("/get_user_info")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public List<GetUserInfoResponse> get_User_Info(GetUserInfoRequest request) throws Exception;

This return me a list of GetUserInfoResponse like this

enter image description here

Question : Is it possible to get a JSON like this ???? :

enter image description here

Class : GetUserInfoResponse

package com.audaxis.compiere.ws.bean.response;

//Same imports

@XmlRootElement(name="infos")
@XmlType(propOrder={"key", "values"})
public class GetUserInfoResponse {

    private int key;
    private List<GetUserInfo> values;

   //Same Constructor
   //Same getters && setters
}

Class : GetUserInfo

   package com.audaxis.compiere.ws.bean;
    //Same imports

    @XmlRootElement(name="values")
    @XmlType(propOrder={"columnName", "old_value", "new_value", "status", "motif"})
    public class GetUserInfo {

        private String columnName;
        private String old_value;
        private String new_value;
        private String status;
        private String motif;

//Same Constructor
//Same getters && setters

 }

And This is my program :

methode(){
    List<GetUserInfoResponse> responses = new ArrayList<GetUserInfoResponse>();
    while(rs.next()){
    GetUserInfoResponse response = new GetUserInfoResponse();
      for (X_Z_WS_Column column : columns) {
        GetUserInfo info = new GetUserInfo();
        //setinfo
        infos.add(info);
    }
    response.setValues(infos);
    responses.add(response);
   }
   return responses
 }

1 Answer 1

1

Here is the basic java code to make json object and populate it.

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class test {
    public static void main(String[] args) {

        JSONObject obj = new JSONObject();
        obj.put("key", "city");

        JSONArray list = new JSONArray();
        list.add("Delhi");
        list.add("Mumbai");
        list.add("Bangalore");
        obj.put("value", list);

        System.out.print(obj);

    }

}

If you have any doubts about the format you are welcome to ask. Output is:

{"value":["Delhi","Mumbai","Bangalore"],"key":"city"}

Sign up to request clarification or add additional context in comments.

5 Comments

i dont create json like this, i instance bean above an the lib jackson i think translate bean to json
Can you post your whole program, I am not getting you?
What is the value of columns in this line? " for (X_Z_WS_Column column : columns) ". Where are you poplating the object?
this return column that contain values (every column make a value)
Then you can just remove the "infos column" wherever you are populating the columns.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.