From 603749d2959fb46a18d02971885b4618e401da55 Mon Sep 17 00:00:00 2001 From: "mail_yanpeng@163.com" Date: Tue, 25 Aug 2020 11:43:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88swagger=203.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../java/com/aiprose/mbp/MbpApplication.java | 2 + .../mbp/conf/SwaggerConfiguration.java | 75 +++++++++---------- .../com/aiprose/mbp/conf/WebMvcConfig.java | 25 +++---- 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/build.gradle b/build.gradle index 8365096..c758391 100644 --- a/build.gradle +++ b/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 { diff --git a/src/main/java/com/aiprose/mbp/MbpApplication.java b/src/main/java/com/aiprose/mbp/MbpApplication.java index bbf4779..f5776bf 100644 --- a/src/main/java/com/aiprose/mbp/MbpApplication.java +++ b/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) { diff --git a/src/main/java/com/aiprose/mbp/conf/SwaggerConfiguration.java b/src/main/java/com/aiprose/mbp/conf/SwaggerConfiguration.java index 8d2251e..cda902f 100644 --- a/src/main/java/com/aiprose/mbp/conf/SwaggerConfiguration.java +++ b/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(); -// } -//} \ No newline at end of file +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(); + } +} \ No newline at end of file diff --git a/src/main/java/com/aiprose/mbp/conf/WebMvcConfig.java b/src/main/java/com/aiprose/mbp/conf/WebMvcConfig.java index ea4178f..2fbc0e9 100644 --- a/src/main/java/com/aiprose/mbp/conf/WebMvcConfig.java +++ b/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"); + } + }