我的问题是ClassB
是我必须返回的普通bean类(使用@ResponseBody
的Json数据格式)简单错误消息。 ClassA是一个Entity bean ,没有将错误消息设置为fstList, fstListMap
的设置方法和获取方法。如果fstListMap,errsecstMap
对象有任何替代解决方案或任何合并解决方案,以返回通用Map Object。如果只有任何更改,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;
}
如何返回errsecstMap?
没有参数可以设置errMessage
实体中的ClassA
。我想返回单个Map对象,如果满足或不满足条件。有什么解决方案可以根据上述代码返回通用Map Object来返回?
答案 0 :(得分:0)
尝试一下(未编译)-
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)像这样修改您现有的课程-
@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);
}
}
显然,可以根据您的用户情况轻松优化上述代码。这是一个简单的实现。