本文主要是介绍Optional .ofNullable()和flatMap()构造请求参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、构造的请求参数
{
"params": {
"filter": {
"joiner": "and",
"conditions": [
{
"conditionName": "code",
"conditionValues": [
"A00000925"
],
"operator": "="
},
{
"conditionName": "version",
"conditionValues": [
"A"
],
"operator": "="
}
]
},
"orderBy": "createTime",
"sort": "DESC",
"isNeedTotal": true
}
}
2、实体
import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import java.util.ArrayList; import java.util.List;/*** 条件DTO* @author xxx* @since xxx*/ @NoArgsConstructor @AllArgsConstructor @Getter @Setter public class ConditionsDTO {// 查询条件private List<ConditionDTO> conditions = new ArrayList<>();@NoArgsConstructor@Getter@Setterpublic static class ConditionDTO {// 条件名-实体对象属性名private String conditionName;// 查询条件值private List<String> conditionValues;// 操作符,例如:=private String operator;} }
3、实现
public static JSONObject assembleFindCommonParam(ConditionsDTO conditionsDTO) {JSONObject json = new JSONObject();JSONObject paramsJson = new JSONObject();JSONObject filterJson = new JSONObject();filterJson.put(DmeParamConstant.JOINER, DmeParamConstant.AND);JSONArray jsonArray = new JSONArray();Optional .ofNullable(conditionsDTO).map(obj -> obj.getConditions()).stream().flatMap(Collection::stream).filter(Objects::nonNull).forEach(obj -> {JSONObject conditionJson = new JSONObject();conditionJson.put(DmeParamConstant.CONDITION_NAME, obj.getConditionName());conditionJson.put(DmeParamConstant.CONDITION_VALUES, obj.getConditionValues());conditionJson.put(DmeParamConstant.OPERATOR, obj.getOperator());jsonArray.add(conditionJson);});filterJson.put(DmeParamConstant.CONDITIONS, jsonArray);paramsJson.put(DmeParamConstant.FILTER, filterJson);paramsJson.put(DmeParamConstant.ORDER_BY, DmeParamConstant.CREATE_TIME);paramsJson.put(DmeParamConstant.SORT, DmeParamConstant.DESC);paramsJson.put(DmeParamConstant.IS_NEED_TOTAL, true);json.put(DmeParamConstant.PARAMS, paramsJson);return json; }
这篇关于Optional .ofNullable()和flatMap()构造请求参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!