public class ParameterTypeReader implements ParameterBuilderPlugin {
@Override
public void apply(ParameterContext context) {
context.parameterBuilder().parameterType(findParameterType(context));
}
@Override
public boolean supports(DocumentationType delimiter) {
return true;
}
public static String findParameterType(ParameterContext parameterContext) {
ResolvedMethodParameter resolvedMethodParameter = parameterContext.resolvedMethodParameter();
ResolvedType parameterType = resolvedMethodParameter.getParameterType();
parameterType = parameterContext.alternateFor(parameterType);
//Multi-part file trumps any other annotations
if (MultipartFile.class.isAssignableFrom(parameterType.getErasedType())) {
return "form";
}
if (resolvedMethodParameter.hasParameterAnnotation(PathVariable.class)) {
return "path";
} else if (resolvedMethodParameter.hasParameterAnnotation(ModelAttribute.class)) {
return queryOrForm(parameterContext.getOperationContext());
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestBody.class)) {
return "body";
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestParam.class)) {
return queryOrForm(parameterContext.getOperationContext());
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestHeader.class)) {
return "header";
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestPart.class)) {
return "form";
}
if (!resolvedMethodParameter.hasParameterAnnotations()) {
return queryOrForm(parameterContext.getOperationContext());
}
return "body";
}
private static String queryOrForm(OperationContext context) {
if (context.consumes().contains(MediaType.APPLICATION_FORM_URLENCODED) && context.httpMethod() == HttpMethod
.POST) {
return "form";
}
return "query";
}
}
swagger2 paramType
最新推荐文章于 2022-05-17 18:22:19 发布