From 25eb223fc49478614162b3af7c9b830e8157df42 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Sun, 5 Mar 2023 18:19:47 +0800 Subject: [PATCH] =?UTF-8?q?swagger=5Fdemo=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- swagger_study/pom.xml | 12 +++++ .../swagger_study/config/SwaggerConfig.java | 48 +++++++++++++++---- .../controller/DemoController.java | 42 ++++++++++++++++ .../controller/HelloController.java | 20 ++++++++ .../bnblogs/swagger_study/pojo/Student.java | 24 ++++++++++ .../src/main/resources/application-dev.yml | 2 + .../src/main/resources/application-prod.yml | 2 + .../src/main/resources/application-test.yml | 2 + .../src/main/resources/application.yml | 5 ++ 9 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java create mode 100644 swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java create mode 100644 swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java create mode 100644 swagger_study/src/main/resources/application-dev.yml create mode 100644 swagger_study/src/main/resources/application-prod.yml create mode 100644 swagger_study/src/main/resources/application-test.yml diff --git a/swagger_study/pom.xml b/swagger_study/pom.xml index 061915f..7671587 100644 --- a/swagger_study/pom.xml +++ b/swagger_study/pom.xml @@ -26,6 +26,18 @@ io.springfox springfox-swagger2 2.9.2 + + + io.swagger + swagger-models + + + + + + io.swagger + swagger-models + 1.5.22 diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java index 7c8d010..ce19a1d 100644 --- a/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java @@ -1,6 +1,12 @@ package cc.bnblogs.swagger_study.config; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.core.env.Profiles; +import springfox.documentation.builders.ParameterBuilder; +import springfox.documentation.schema.ModelRef; +import springfox.documentation.service.Parameter; import springfox.documentation.swagger2.annotations.EnableSwagger2; import org.springframework.context.annotation.Bean; import springfox.documentation.builders.ApiInfoBuilder; @@ -11,9 +17,12 @@ import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; +import java.util.ArrayList; +import java.util.List; + /** - * @description: + * @description: 配置Swagger * @author: zfp@bnblogs.cc * @date: 2023/3/5 0:00 */ @@ -21,17 +30,36 @@ import springfox.documentation.spring.web.plugins.Docket; @EnableSwagger2 @Configuration public class SwaggerConfig { - @Bean - public Docket createRestApi() { - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .select() - //为当前包路径,控制器类包 - .apis(RequestHandlerSelectors.basePackage("cc.bnblogs.swagger_study.controller")) - .paths(PathSelectors.any()) + public Docket docket(){ + // 新建一个全局参数 + Parameter token = new ParameterBuilder().name("token") + // 描述信息 + .description("用户登录令牌") + // 该参数放在请求头 + .parameterType("header") // 也可以设置query参数,相当于@RequestParam + // 参数的类型 + .modelRef(new ModelRef("String")) + // 该参数必填 + .required(true) .build(); + + List parameters = new ArrayList<>(); + parameters.add(token); + + return new Docket(DocumentationType.SWAGGER_2) + .globalOperationParameters(parameters).apiInfo(apiInfo()); + } + +// @Bean +// public Docket RestfulApi() { +// return new Docket(DocumentationType.SWAGGER_2) +// .apiInfo(apiInfo()) +// .select() +// .apis(RequestHandlerSelectors.basePackage("cc.bnblogs.swagger_study.controller")) +// .paths(PathSelectors.any()).build(); +// } private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 @@ -41,7 +69,7 @@ public class SwaggerConfig { //版本号 .version("1.0") //描述 - .description("演示系统API描述") + .description("学习使用Swagger") .build(); } } diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java new file mode 100644 index 0000000..e8319a0 --- /dev/null +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java @@ -0,0 +1,42 @@ +package cc.bnblogs.swagger_study.controller; + +import cc.bnblogs.swagger_study.pojo.Student; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; + +@Api(tags = "demo接口") +@RestController +@RequestMapping("/demo/") +public class DemoController { + @GetMapping + @ApiOperation("输出hello world") + public String greet() { + return "Hello, World"; + } + + @PostMapping("/{id}/") + @ApiOperation("提交一个id") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "编号", example = "1"), + @ApiImplicitParam(name = "name", value = "姓名", example = "admin") + }) + public Integer addInfo(@RequestParam Integer id, + @RequestParam String name) { + System.out.println(name); + return id; + } + @PostMapping("/stu/add/") + public Student addStudent(@RequestBody Student stu) { + return stu; + } + + + @PostMapping("/stu/test/") + public Student addStudentTest(Student stu) { + return stu; + } + +} \ No newline at end of file diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java new file mode 100644 index 0000000..be44ffd --- /dev/null +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java @@ -0,0 +1,20 @@ +package cc.bnblogs.swagger_study.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +/** + * @description: + * @author: zfp@bnblogs.cc + * @date: 2023/3/5 15:24 + */ +@RestController +@RequestMapping("/hello/") +public class HelloController { + @GetMapping + public String greet() { + return "Hello, World"; + } +} diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java new file mode 100644 index 0000000..f59febb --- /dev/null +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java @@ -0,0 +1,24 @@ +package cc.bnblogs.swagger_study.pojo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @description: 学生表 + * @author: zfp@bnblogs.cc + * @date: 2023/3/5 14:13 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@ApiModel("学生表") +public class Student { + @ApiModelProperty("学号") + private Integer id; + @ApiModelProperty("姓名") + private String name; + +} diff --git a/swagger_study/src/main/resources/application-dev.yml b/swagger_study/src/main/resources/application-dev.yml new file mode 100644 index 0000000..54b155f --- /dev/null +++ b/swagger_study/src/main/resources/application-dev.yml @@ -0,0 +1,2 @@ +server: + port: 8081 \ No newline at end of file diff --git a/swagger_study/src/main/resources/application-prod.yml b/swagger_study/src/main/resources/application-prod.yml new file mode 100644 index 0000000..9899989 --- /dev/null +++ b/swagger_study/src/main/resources/application-prod.yml @@ -0,0 +1,2 @@ +server: + port: 8083 \ No newline at end of file diff --git a/swagger_study/src/main/resources/application-test.yml b/swagger_study/src/main/resources/application-test.yml new file mode 100644 index 0000000..0884131 --- /dev/null +++ b/swagger_study/src/main/resources/application-test.yml @@ -0,0 +1,2 @@ +server: + port: 8082 \ No newline at end of file diff --git a/swagger_study/src/main/resources/application.yml b/swagger_study/src/main/resources/application.yml index e69de29..1e42909 100644 --- a/swagger_study/src/main/resources/application.yml +++ b/swagger_study/src/main/resources/application.yml @@ -0,0 +1,5 @@ +#spring: +# profiles: +# active: test # 当前环境为dev +server: + port: 8080 \ No newline at end of file