pub.codex.apix.operation.OperationRequestBodyReader Maven / Gradle / Ivy
package pub.codex.apix.operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import pub.codex.apix.annotations.ApiGroup;
import pub.codex.apix.annotations.ApiModelProperty;
import pub.codex.apix.context.OperationContext;
import pub.codex.apix.wrapper.VaildComponent;
import javax.validation.Valid;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
/**
* 处理 RequestMapping 的 requestBody
* 处理非基础类型&String
*
* 处理这几种情况
* 所有参与计算的对象,必须被 @RequestBody标注
*
*
* 1、@Valid :
* ApiModelProperty (选填)
* Notnull(必填)
*
* 2、@Validate(g1) :
* ApiModelProperty/ApiModelProperty(g1)/(空)/*ApiModelProperty(g2)*,Notnull(g1)(必填)
* ApiModelProperty(g1),Notnull/ Notnull(g2)/(空)(选填)
*
*
* 3、@ApiGroup(g1) :
* ApiModelProperty(g1),NotNull/(空) (选填)
*
*
* 4、@Validate(g1) ,@ApiGroup(g1):
* ApiModelProperty/(空)/*ApiModelProperty(g2)*,Notnull(g1)(必填)
* ApiModelProperty(g1),Notnull/(空)/ Notnull(g2)(选填)
*
*
* 5、@Validate(g1) ,@ApiGroup(g2):
* ApiModelProperty(g1)(选填)
* ApiModelProperty(g2)(选填)
* Notnull(g1)(必填)
* ApiModelProperty,Notnull(g1)(必填)*4.1
* ApiModelProperty(g1),Notnull(选填)*4.2
* ApiModelProperty(g2),Notnull(选填)
* ApiModelProperty(g1),Notnull(g1) (必填)
* ApiModelProperty(g2),Notnull(g2) (选填)
* ApiModelProperty(g1),Notnull(g2) (选填)*4.2
* ApiModelProperty(g2),Notnull(g1) (必填)*5.7
*
* 思路:">":优先级 "-->":决定
* 0、vaild
* 1、Validate>ApiGroup
* 2、Validate-->Notnull (1、匹配成功,必填,且后续操作忽略)
* -->ApiModelProperty (2、验证匹配成功,选填)
*
* ApiGroup-->ApiModelProperty (3、Validate完全匹配失败,尝试匹配,如果成功:选填)
*
* @see {@ReqyestParam} 包含必填验证
*/
@Component
@Order(Ordered.OperationRequestBodyReader)
public class OperationRequestBodyReader implements OperationBuilderPlugin {
@Autowired
private VaildComponent vaildComponent;
@Override
public void apply(OperationContext context) {
// 处理requestBody 请求参数
requestBodyHandel(context);
}
/**
* 处理requestBody 请求参数
*
* POST的情况下处理 处理requestBody
* 其他提交方式待定
*
* @param context
*/
private void requestBodyHandel(OperationContext context) {
// 处理POST请求
if (HttpMethod.POST.equals(context.httpMethod()) || HttpMethod.PUT.equals(context.httpMethod())) {
requestBodyParamsHandle(context);
}
}
/**
* 遍历处理 requestBody 参数
*
* 虽然Spring官方 请求mapping方法只允许参数出现一个 requestBody参数,
* 为了契合方法返回类型,还是使用遍历
*
* @param context
*/
private void requestBodyParamsHandle(OperationContext context) {
List methodParameterList = context.getParameterAnnotation(RequestBody.class);
methodParameterList.stream().forEach(methodParameter -> {
Map map = newHashMap();
map.put("fields", paramsHandle(methodParameter));
context.operationBuilder().setParamsBody(map);
});
}
/**
* 处理 requestBody的参数
*
* @param methodParameter
* @return
*/
private List