简体   繁体   中英

How to put List data with different generic type with same Return type?

My problem is that ClassB is a common bean class i have to return(Json data format using @ResponseBody ) simple error message. ClassA is an Entity bean there no setter and getter method to set the error message to the fstList, fstListMap . If there is any alternate solution or else any merging solution for fstListMap,errsecstMap Object to return common Map Object . If any changes only ClassA bean is changeable but ClassB Not changeable.

@RequestMapping(value="getlistOfData",method=RequestMethod.GET)
@ResponseBody
public Map<String, List<ClassA>>
getListOfData(@RequestParam(value="param1")String
                      param1,@RequestParam(value="param2")int param2){

    List<ClassA> fstList=new ArrayList<ClassA>();//ClassA is an hibernate Entity
    Map<String, List<ClassA>> fstListMap=new HashMap<String, List<ClassA>>();

    List<ClassB> errsecstList=new ArrayList<ClassB>();//It is simple Bean class
    Map<String, List<ClassB>> errsecstMap=new HashMap<String, List<ClassB>>();
    ClassB clB=new ClassB();
    boolean isvalid=true;
    try{


        isvalid=newllbean.getisInvalidAge();//This value return drools (set in rule file)
        if(isvalid){//if true then go to this condition
            fstList=serviceClass.getfstList()
            fstListMap.put("fstList", fstList);
            System.out.println("fstList Size: "+fstListMap.size());
            System.out.println("fstList : "+fstListMap);//Am getting this data
        }else{
            String errMessage=clB.setErrorMessage("Age Not valid...");
            errsecstList.add(clB.getErrorMessage());
            errsecstMap.put("errorMessage",errsecstList);
            System.out.println("errMessage Size: "+errsecstMap.size());
            System.out.println("errMessage : "+errsecstMap);//I am getting this value
        }
    }catch(Exception e){
        System.out.println("Exception"+e);
    }
    return fstListMap;
}

how to return errsecstMap?

there is a no parameter to set the errMessage in ClassA entity. I want to return single Map object either if or else condition. There is any solution to return common Map Object to return based on the above code?

Try this (this is not compiled) -

1) Create a Response class like this -

import java.util.List;

public class ResponseClass<T> {

    public String str;
    public List<T> list;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

}

2) Modify your existing class like this -

@RequestMapping(value="getlistOfData",method=RequestMethod.GET)
@ResponseBody
public ResponseClass<T>
getListOfData(@RequestParam(value="param1")String
                      param1,@RequestParam(value="param2")int param2){

    List<ClassA> fstList=new ArrayList<ClassA>();//ClassA is an hibernate Entity
    ResponseClass<ClassA> responseA = new ResponseClass<>();

    List<ClassB> errsecstList=new ArrayList<ClassB>();//It is simple Bean class
    ResponseClass<ClassB> responseB = new ResponseClass<>();

    ClassB clB=new ClassB();
    boolean isvalid=true;

    try{


        isvalid=newllbean.getisInvalidAge();//This value return drools (set in rule file)
        if(isvalid){//if true then go to this condition
            fstList=serviceClass.getfstList()
            responseA.setStr("fstList");
            responseA.setList(fstList);
            System.out.println("fstList Size: "+fstListMap.size());
            System.out.println("fstList : "+fstListMap);//Am getting this data
            return responseA;
        }else{
            String errMessage=clB.setErrorMessage("Age Not valid...");
            errsecstList.add(clB.getErrorMessage());
            responseB.setStr("errorMessage");
            responseB.setList(errsecstList);
            System.out.println("errMessage Size: "+errsecstMap.size());
            System.out.println("errMessage : "+errsecstMap);//I am getting this value
            return responseB;
        }
    }catch(Exception e){
        System.out.println("Exception"+e);
    }
}

Obviously, above code can be easily optimized based on your user case. This is a simple implementation.

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