简体   繁体   English

如何将具有相同返回类型的不同通用类型的列表数据放入?

[英]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. 我的问题是ClassB是我必须返回的普通bean类(使用@ResponseBody Json数据格式)简单错误消息。 ClassA is an Entity bean there no setter and getter method to set the error message to the fstList, fstListMap . ClassA是一个Entity bean ,没有设置器和获取器方法将错误消息设置为fstList, fstListMap If there is any alternate solution or else any merging solution for fstListMap,errsecstMap Object to return common Map Object . 如果fstListMap,errsecstMap有任何替代解决方案或其他合并解决方案fstListMap,errsecstMap Object返回公共Map Object。 If any changes only ClassA bean is changeable but ClassB Not changeable. 如果任何变化只ClassA bean是多变的 ,但ClassB 改变。

@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? 如何返回errsecstMap?

there is a no parameter to set the errMessage in ClassA entity. 没有参数可errMessageClassA实体中设置errMessage I want to return single Map object either if or else condition. 我想返回单个Map对象,如果满足或不满足条件。 There is any solution to return common Map Object to return based on the above code? 有什么解决方案可以根据上述代码返回通用Map Object来返回?

Try this (this is not compiled) - 试试这个(这是没有编译)-

1) Create a Response class like this - 1)创建一个这样的Response类-

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 - 2)像这样修改您现有的课程-

@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. 这是一个简单的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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