背景:
此处为花里胡哨的用法,使用用方法返回的结果作为下一个方法的参数的调用方式
import java.util.function.Function;
@RestController
@RequestMapping("function")
public class TestFunctionController {
@RequestMapping("/do")
public Object doFunction() throws Exception {
Map<String, Object> param = new HashMap<>();
param.put("1", 1);
System.out.println("-----------------------0");
//boolean b1 = requestMapper(param, (resultDto) -> afterDo((CommonResultDto<FunctionTestDto>) resultDto), FunctionTestDto.class);
Object dealOneLastRetrun = dealOne(param, this::afterDo, FunctionTestDto.class);
System.out.println("-----------------------5");
return dealOneLastRetrun;
}
/**
* 请求公用请求类
*
* @param params 请求参数
* @param fun 请求结果 处理方法
* @param tClass 返回集合对象
* @return
* @throws Exception
*/
public <T, R> Object dealOne(Map params, Function<CommonResultDto<T>, R> fun, Class<T> tClass) throws Exception {
System.out.println("-----------------------1");
String i = params.get("1").toString();
//CommonResultDto作为afterDo方法的参数
CommonResultDto afterDoParam = new CommonResultDto();
afterDoParam.setCode(10).setMessage("pass by");
List<FunctionTestDto> list = new ArrayList<>();
FunctionTestDto test = new FunctionTestDto().setAge(11).setName("test");
list.add(test);
afterDoParam.setList(list);
//设置方法参数
System.out.println("-----------------------2");
Object afterDoReturn = fun.apply(afterDoParam);
System.out.println("-----------------------4 afterDoReturn:" + afterDoReturn.toString());
Object dealOneLastRetrun = afterDoReturn;
return dealOneLastRetrun;
}
/**
* 获得前一个方法的参数后,执行
*
* @param resultDto
* @return
*/
Object afterDo(CommonResultDto<FunctionTestDto> resultDto) {
System.out.println("-----------------------3");
Integer code = resultDto.getCode();
List<FunctionTestDto> list = resultDto.getList();
FunctionTestDto dto = list.get(0);
Map<String, Object> beanToMap = BeanUtil.beanToMap(dto);
return beanToMap;
}
}
中间参数对象
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
public class FunctionTestDto implements Serializable {
private Integer age;
private String name;
}
中间参数对象2
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.List;
@Data
@Accessors(chain = true)
public class CommonResultDto <T> implements Serializable {
private Integer code;
private String message;
private Integer totalCount;
private List<T> list;
}