Commit d9bf06d8 authored by lidongdai's avatar lidongdai
Browse files

添加api文档

parent bb1fa37e
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -168,12 +168,6 @@
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-jaxrs</artifactId>
      <version>1.5.12</version>
    </dependency>

  </dependencies>
  <build>
    <plugins>
+7 −5
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ package cn.escheduler.api.configuration;
import cn.escheduler.api.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

@@ -49,12 +49,12 @@ public class AppConfiguration implements WebMvcConfigurer {
  /**
   * Cookie
   */
  @Bean
  @Bean(name = "localeResolver")
  public LocaleResolver localeResolver() {
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setCookieName(LOCALE_LANGUAGE_COOKIE);
    /** set default locale **/
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    localeResolver.setDefaultLocale(Locale.US);
    /** set cookie max age **/
    localeResolver.setCookieMaxAge(COOKIE_MAX_AGE);
    return localeResolver;
@@ -64,7 +64,7 @@ public class AppConfiguration implements WebMvcConfigurer {
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    /**  **/
    lci.setParamName("lang");
    lci.setParamName("language");

    return lci;
  }
@@ -72,14 +72,16 @@ public class AppConfiguration implements WebMvcConfigurer {

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN,"/swagger-resources/**", "/webjars/**", "/v2/**", "/doc.html", "*.html");
    //i18n
    registry.addInterceptor(localeChangeInterceptor());

    registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN,"/swagger-resources/**", "/webjars/**", "/v2/**", "/doc.html", "*.html");
  }


  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }
+0 −109
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.escheduler.api.configuration;

import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.AllowableValues;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.DescriptionResolver;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.util.List;
import java.util.Locale;

import static com.google.common.base.Strings.emptyToNull;
import static springfox.documentation.schema.Types.isBaseType;
import static springfox.documentation.swagger.common.SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER;
import static springfox.documentation.swagger.readers.parameter.Examples.examples;
import static springfox.documentation.swagger.schema.ApiModelProperties.allowableValueFromString;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE - 10)
public class SwaggerApiImplicitParamPlugin implements OperationBuilderPlugin {

    @Autowired
    private DescriptionResolver descriptions;

    @Autowired
    private MessageSource messageSource;

    static Parameter implicitParameter(MessageSource messageSource, DescriptionResolver descriptions, ApiImplicitParam param) {
        Locale locale = LocaleContextHolder.getLocale();

        ModelRef modelRef = maybeGetModelRef(param);
        return new ParameterBuilder()
                .name(param.name())
                .description(descriptions.resolve(messageSource.getMessage(param.value(), null, locale)))
                .defaultValue(param.defaultValue())
                .required(param.required())
                .allowMultiple(param.allowMultiple())
                .modelRef(modelRef)
                .allowableValues(allowableValueFromString(param.allowableValues()))
                .parameterType(emptyToNull(param.paramType()))
                .parameterAccess(param.access())
                .order(SWAGGER_PLUGIN_ORDER)
                .scalarExample(param.example())
                .complexExamples(examples(param.examples()))
                .build();
    }

    private static ModelRef maybeGetModelRef(ApiImplicitParam param) {
        String dataType = MoreObjects.firstNonNull(emptyToNull(param.dataType()), "string");
        AllowableValues allowableValues = null;
        if (isBaseType(dataType)) {
            allowableValues = allowableValueFromString(param.allowableValues());
        }
        if (param.allowMultiple()) {
            return new ModelRef("", new ModelRef(dataType, allowableValues));
        }
        return new ModelRef(dataType, allowableValues);
    }

    @Override
    public void apply(OperationContext context) {
        context.operationBuilder().parameters(readParameters(context));
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }

    private List<Parameter> readParameters(OperationContext context) {
        Optional<ApiImplicitParam> annotation = context.findAnnotation(ApiImplicitParam.class);
        List<Parameter> parameters = Lists.newArrayList();
        if (annotation.isPresent()) {
            parameters.add(implicitParameter(messageSource, descriptions, annotation.get()));
        }
        return parameters;
    }

}
 No newline at end of file
+0 −71
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.escheduler.api.configuration;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.DescriptionResolver;

import java.util.List;

import static springfox.documentation.swagger.common.SwaggerPluginSupport.pluginDoesApply;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE - 10)
public class SwaggerApiImplicitParamsPlugin implements OperationBuilderPlugin {

    @Autowired
    private DescriptionResolver descriptions;
    @Autowired
    private MessageSource messageSource;

    @Override
    public void apply(OperationContext context) {
        context.operationBuilder().parameters(readParameters(context));
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return pluginDoesApply(delimiter);
    }

    private List<Parameter> readParameters(OperationContext context) {
        Optional<ApiImplicitParams> annotation = context.findAnnotation(ApiImplicitParams.class);

        List<Parameter> parameters = Lists.newArrayList();
        if (annotation.isPresent()) {
            for (ApiImplicitParam param : annotation.get().value()) {
                parameters.add(SwaggerApiImplicitParamPlugin.implicitParameter(messageSource, descriptions, param));
            }
        }

        return parameters;
    }


}
 No newline at end of file
+0 −74
Original line number Diff line number Diff line
package cn.escheduler.api.configuration;



import com.fasterxml.classmate.TypeResolver;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.schema.ModelReference;
import springfox.documentation.schema.TypeNameExtractor;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static springfox.documentation.schema.ResolvedTypes.*;
import static springfox.documentation.swagger.common.SwaggerPluginSupport.*;

/**
 * NOTE : not useful
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE - 10)
public class SwaggerApiModelPlugin implements ModelBuilderPlugin {

    @Autowired
    private TypeResolver typeResolver;
    @Autowired
    private TypeNameExtractor typeNameExtractor;
    @Autowired
    private MessageSource messageSource;

    @Override
    public void apply(ModelContext context) {
        ApiModel annotation = AnnotationUtils.findAnnotation(forClass(context), ApiModel.class);
        if (annotation != null) {
            List<ModelReference> modelRefs = new ArrayList<ModelReference>();
            for (Class<?> each : annotation.subTypes()) {
                modelRefs.add(modelRefFactory(context, typeNameExtractor)
                        .apply(typeResolver.resolve(each)));
            }
            Locale locale = LocaleContextHolder.getLocale();

            context.getBuilder()
                    .description(messageSource.getMessage(annotation.description(), null, locale))
                    .discriminator(annotation.discriminator())
                    .subTypes(modelRefs);
        }
    }

    private Class<?> forClass(ModelContext context) {
        return typeResolver.resolve(context.getType()).getErasedType();
    }


//    @Override
//    public boolean supports(DocumentationType delimiter) {
//        return pluginDoesApply(delimiter);
//    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return true;
    }
}
Loading