Browse Source

整合swagger 3.0

master
mail_yanpeng@163.com 4 years ago
parent
commit
603749d295
  1. 2
      build.gradle
  2. 2
      src/main/java/com/aiprose/mbp/MbpApplication.java
  3. 75
      src/main/java/com/aiprose/mbp/conf/SwaggerConfiguration.java
  4. 25
      src/main/java/com/aiprose/mbp/conf/WebMvcConfig.java

2
build.gradle

@ -28,6 +28,8 @@ dependencies {
// compile group: 'com.github.xiaoymin', name: 'knife4j-spring-boot-starter', version: '2.0.4'
// implementation 'com.github.xiaoymin:knife4j-spring-boot-starter:2.0.2'
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
}
test {

2
src/main/java/com/aiprose/mbp/MbpApplication.java

@ -6,10 +6,12 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.oas.annotations.EnableOpenApi;
@MapperScan({"com.aiprose.mbp.mapper","com.gitee.sunchenbin.mybatis.actable.dao.*"})
@ComponentScan({"com.gitee.sunchenbin.mybatis.actable.manager.*","com.aiprose.mbp.*"})
@SpringBootApplication
@EnableOpenApi
public class MbpApplication {
public static void main(String[] args) {

75
src/main/java/com/aiprose/mbp/conf/SwaggerConfiguration.java

@ -1,38 +1,37 @@
//package com.aiprose.mbp.conf;
//
//import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.ApiInfo;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
//@Configuration
//@EnableSwagger2
//@EnableKnife4j
//@Import(BeanValidatorPluginsConfiguration.class)
//public class SwaggerConfiguration {
// @Bean
// public Docket createRestApi() {
// return new Docket(DocumentationType.SWAGGER_2)
// .apiInfo(apiInfo())
// .select()
// .apis(RequestHandlerSelectors.basePackage("com.aiprose.mbp.controller"))
// .paths(PathSelectors.any())
// .build();
// }
//
// private ApiInfo apiInfo() {
// return new ApiInfoBuilder()
// .title("swagger-bootstrap-ui RESTful APIs")
// .description("swagger-bootstrap-ui")
// .version("1.0")
// .build();
// }
//}
package com.aiprose.mbp.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.aiprose.mbp.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot mybatis plus框架demo")
.description("springboot mybatis plus框架demo")
.contact(new Contact("nelson", "https://www.aiprose.com/", "mail_yanpeng@163.com"))
.version("1.0")
.build();
}
}

25
src/main/java/com/aiprose/mbp/conf/WebMvcConfig.java

@ -3,6 +3,7 @@ package com.aiprose.mbp.conf;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
@ -17,21 +18,17 @@ public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/web/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.
addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
super.addResourceHandlers(registry);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/swagger-ui/")
.setViewName("forward:/swagger-ui/index.html");
}
}

Loading…
Cancel
Save