From 9c968454e1dd4e4c5e111f80b318ecc7ce3b3729 Mon Sep 17 00:00:00 2001 From: guyaojiang Date: Sun, 9 Jan 2022 17:26:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0swagger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- austin-web/pom.xml | 10 ++++ .../java3y/austin/config/SwaggerConfig.java | 52 +++++++++++++++++++ .../controller/MessageTemplateController.java | 7 +++ pom.xml | 11 ++++ 4 files changed, 80 insertions(+) create mode 100644 austin-web/src/main/java/com/java3y/austin/config/SwaggerConfig.java diff --git a/austin-web/pom.xml b/austin-web/pom.xml index aca3fe9..91b59a3 100644 --- a/austin-web/pom.xml +++ b/austin-web/pom.xml @@ -42,6 +42,16 @@ micrometer-registry-prometheus + + io.springfox + springfox-swagger2 + + + + io.springfox + springfox-swagger-ui + + diff --git a/austin-web/src/main/java/com/java3y/austin/config/SwaggerConfig.java b/austin-web/src/main/java/com/java3y/austin/config/SwaggerConfig.java new file mode 100644 index 0000000..d96f99c --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/config/SwaggerConfig.java @@ -0,0 +1,52 @@ +package com.java3y.austin.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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; + +/** + * @program: austin + * @description: swagger + * @author: YorickGu + * @create: 2022-01-09 17:22 + **/ + +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + // 指定构建api文档的详细信息的方法:apiInfo() + .apiInfo(apiInfo()) + .select() + // 指定要生成api接口的包路径 + .apis(RequestHandlerSelectors.basePackage("com.java3y.austin.controller")) + //使用了 @ApiOperation 注解的方法生成api接口文档 + //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) + .paths(PathSelectors.any()) + //可以根据url路径设置哪些请求加入文档,忽略哪些请求 + .build(); + } + + /** + * 设置api文档的详细信息 + */ + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + // 标题 + .title("Austin") + // 接口描述 + .description("Austin") + // 版本信息 + .version("1.0") + // 构建 + .build(); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java b/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java index 1f85f88..dd0b8c5 100644 --- a/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java +++ b/austin-web/src/main/java/com/java3y/austin/controller/MessageTemplateController.java @@ -4,8 +4,11 @@ import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSON; import com.java3y.austin.dao.MessageTemplateDao; import com.java3y.austin.domain.MessageTemplate; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -16,6 +19,8 @@ import org.springframework.web.bind.annotation.RestController; * @author 3y */ @RestController +@RequestMapping("/Message") +@Api("发送消息") public class MessageTemplateController { @Autowired @@ -25,6 +30,7 @@ public class MessageTemplateController { * test insert */ @GetMapping("/insert") + @ApiOperation("/插入数据") public String insert() { MessageTemplate messageTemplate = MessageTemplate.builder() @@ -61,6 +67,7 @@ public class MessageTemplateController { * test query */ @GetMapping("/query") + @ApiOperation("/查找数据") public String query() { Iterable all = messageTemplateDao.findAll(); for (MessageTemplate messageTemplate : all) { diff --git a/pom.xml b/pom.xml index 3965b0b..1fffdfc 100644 --- a/pom.xml +++ b/pom.xml @@ -96,6 +96,17 @@ 1.6.2 + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + +