美文网首页
swagger支持自定义注解

swagger支持自定义注解

作者: 码农梦醒 | 来源:发表于2020-01-15 13:49 被阅读0次

    swagger属性注解@ApimodelProperty支持其它注解.例如@Column属性columnDefinition

    import com.google.common.base.Optional;
    import io.swagger.annotations.ApiModelProperty;
    import org.apache.commons.lang.StringUtils;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.annotation.AnnotatedElementUtils;
    import org.springframework.core.annotation.AnnotationUtils;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
    import springfox.documentation.spring.web.DescriptionResolver;
    import springfox.documentation.swagger.schema.ApiModelPropertyPropertyBuilder;
    
    import javax.persistence.Column;
    import java.util.Set;
    
    @Component
    @Order(1)
    @Primary
    public class ApiModelPropertyPropertyBuilderConf extends ApiModelPropertyPropertyBuilder {
        public ApiModelPropertyPropertyBuilderConf(DescriptionResolver descriptions) {
            super(descriptions);
        }
        @Override
        public void apply(ModelPropertyContext context) {
          super.apply(context);
            if (context.getAnnotatedElement().isPresent()) {
                ApiModelProperty model=AnnotationUtils.getAnnotation(context.getAnnotatedElement().get(), ApiModelProperty.class);
                if(model!=null){
                    return;
                }
            }
           Column column=context.getBeanPropertyDefinition().get().getField().getAnnotation(Column.class);
            if(column!=null){
                if(column.columnDefinition()!=null && column.columnDefinition().length()>0){
                    context.getBuilder().description(column.columnDefinition());
                }
    
            }
    
        }
    }
    
    @Component 
    @Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER) 
    public class OperationBuilderPluginImpl implements OperationBuilderPlugin { 
    
        @Override 
        public void apply(OperationContext context) { 
         Optional<ApiOperation> methodAnnotation = context.findAnnotation(ApiSpecificationInfo.class); 
         if (methodAnnotation.isPresent()) { 
          ApiSpecificationInfo apiSpecificationInfo = methodAnnotation.get(); 
          // do your processing here 
          context.operationBuilder().notes(apiSpecificationInfo.name()); 
         } 
        } 
    
        @Override 
        public boolean supports(DocumentationType delimiter) { 
         return SwaggerPluginSupport.pluginDoesApply(delimiter); 
        } 
    } 
    

    参考

    https://blog.csdn.net/zhou1124/article/details/103773920

    http://cn.voidcc.com/question/p-ufchlvio-uo.html

    相关文章

      网友评论

          本文标题:swagger支持自定义注解

          本文链接:https://www.haomeiwen.com/subject/ihjtzctx.html