下面列出了怎么用io.swagger.annotations.ApiModel的API类实例代码及写法,或者点击链接到github查看源代码。
private void resolveApiParam(ParameterContext context) {
ResolvedType resolvedType = context.resolvedMethodParameter().getParameterType();
Class<?> parameterClass = resolveParamType(resolvedType);
if (parameterClass == null) {
log.warn(StrUtil.concat(resolvedType.getBriefDescription(), "的类型无法被DefaultParamPlugin解析"));
@SuppressWarnings("unused") int a = 1;
return;
}
ApiModel apiModel = parameterClass.getAnnotation(ApiModel.class);
if (apiModel == null) {
if (!BeanUtils.isSimpleProperty(parameterClass)) {
warn(context, parameterClass);
}
return;
}
ParameterBuilder builder = context.parameterBuilder();
builder.name(apiModel.description());
builder.description(descriptions.resolve(apiModel.description()));
builder.allowMultiple(false);
builder.allowEmptyValue(false);
builder.hidden(false);
builder.collectionFormat("");
builder.order(SWAGGER_PLUGIN_ORDER);
}
private String recursiveResolveList(ResolvedType resolvedType) {
if (TypeUtil.isSimpleType(resolvedType)) {
ApiModel apiModel = resolvedType.getErasedType().getAnnotation(ApiModel.class);
if (apiModel != null) {
return apiModel.description();
} else {
return "";
}
} else if (TypeUtil.belongToComplexType(Collection.class, resolvedType)) {
resolvedType = TypeUtil.resolveGenericType(Collection.class, resolvedType).get(0);
String result = recursiveResolveList(resolvedType);
if (result == null) {
return null;
} else {
return StrUtil.concat(result, "的列表");
}
} else {
return null;
}
}
@Override
public String findPropertyDescription(Annotated a)
{
ApiParam apiParam = a.getAnnotation(ApiParam.class);
if (apiParam != null) {
return apiParam.description();
}
ApiModel model = a.getAnnotation(ApiModel.class);
if (model != null && !"".equals(model.description())) {
return model.description();
}
ApiModelProperty prop = a.getAnnotation(ApiModelProperty.class);
if (prop != null) {
return prop.value();
}
return null;
}
@Override
public List<NamedType> findSubtypes(Annotated a)
{
final ApiModel api = a.getAnnotation(ApiModel.class);
if (api != null) {
final Class<?>[] classes = api.subTypes();
final List<NamedType> names = new ArrayList<>(classes.length);
for (Class<?> subType : classes) {
names.add(new NamedType(subType));
}
if (!names.isEmpty()) {
return names;
}
}
return Collections.emptyList();
}
/**
* Look-up a "description" from the given type's {@link ApiModel} annotation's {@code description}.
*
* @param scope targeted type
* @return description (or {@code null})
*/
protected String resolveDescriptionForType(TypeScope scope) {
return Optional.ofNullable(scope.getType())
.map(type -> type.getErasedType().getAnnotation(ApiModel.class))
.map(ApiModel::description)
.filter(description -> !description.isEmpty())
.orElse(null);
}
/**
* Look-up a "title" for the given member or its associated getter/field from the member type's {@link ApiModel} annotation's {@code value}.
*
* @param scope targeted type
* @return title (or {@code null})
*/
protected String resolveTitleForType(TypeScope scope) {
return Optional.ofNullable(scope.getType())
.map(type -> type.getErasedType().getAnnotation(ApiModel.class))
.map(ApiModel::value)
.filter(title -> !title.isEmpty())
.orElse(null);
}
private List<ResolvedType> collectAllTypes(RequestMappingContext context, ResolvedMethodParameter parameter) {
List<ResolvedType> allTypes = newArrayList();
for (ResolvedType type : collectBindingTypes(context.alternateFor(parameter.getParameterType()), newArrayList())) {
ApiModel apiModel = AnnotationUtils.getAnnotation(type.getErasedType(), ApiModel.class);
allTypes.add(type);
if (apiModel != null) {
allTypes.addAll(Arrays.stream(apiModel.subTypes())
.filter(subType -> subType.getAnnotation(ApiModel.class) != type.getErasedType().getAnnotation(ApiModel.class))
.map(typeResolver::resolve).collect(Collectors.toList()));
}
}
return allTypes;
}
@Override
public void apply(ModelPropertyContext context) {
BeanPropertyDefinition definition = context.getBeanPropertyDefinition().orNull();
if (definition == null) {
return;
}
Class<?> modelClass = definition.getAccessor().getDeclaringClass();
Field field = definition.getField().getAnnotated();
if (!modelClass.isAnnotationPresent(ApiModel.class)) {
return;
}
resolveDescription(context, field);
resolveMapEnum(context, field);
resolveExample(context, modelClass, field);
}
@Override
public void process(final CtClass<?> element) {
final CtAnnotation<Annotation> annotation = getFactory().Code().createAnnotation(getFactory().Code().createCtTypeReference(ApiModel.class));
element.addAnnotation(annotation);
log.debug("Add ApiModel to {}", element.getQualifiedName());
}
@Override
public PropertyName findRootName(AnnotatedClass ac) {
ApiModel model = ac.getAnnotation(ApiModel.class);
if (model != null) {
return new PropertyName(model.value());
} else {
return super.findRootName(ac);
}
}
public String getApiDescription() {
if (_apiDescription == null) {
_apiDescription = AnnotationUtil.getAnnotationValueFormMembers(ApiModel.class, ApiModel::value,"", getClazz());
}
return _apiDescription;
}
private void resolveImports(TopLevelClass klass, IntrospectedTable table) {
addImport(klass, Data.class);
addImport(klass, Id.class);
addImport(klass, Table.class);
addImport(klass, Column.class);
addImport(klass, Entity.class);
addImport(klass, ApiModel.class);
addImport(klass, ApiModelProperty.class);
boolean hasNullable = false;
boolean hasNotNull = false;
boolean hasNotBlank = false;
boolean hasLength = false;
for (IntrospectedColumn iter : table.getAllColumns()) {
if (iter.isNullable()) {
hasNullable = true;
} else {
if (iter.isStringColumn()) {
hasNotBlank = true;
} else {
hasNotNull = true;
}
}
if (iter.isStringColumn()) {
if (iter.getLength() != 0) {
hasLength = true;
}
}
}
if (hasNullable) {
addImport(klass, Nullable.class);
}
if (hasNotNull) {
addImport(klass, NotNull.class);
}
if (hasNotBlank) {
addImport(klass, NotBlank.class);
}
if (hasLength) {
addImport(klass, Length.class);
}
}