swagger_demo完成

master
barney 2 years ago
parent 4039f20998
commit 25eb223fc4
  1. 12
      swagger_study/pom.xml
  2. 48
      swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java
  3. 42
      swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java
  4. 20
      swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java
  5. 24
      swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java
  6. 2
      swagger_study/src/main/resources/application-dev.yml
  7. 2
      swagger_study/src/main/resources/application-prod.yml
  8. 2
      swagger_study/src/main/resources/application-test.yml
  9. 5
      swagger_study/src/main/resources/application.yml

@ -26,6 +26,18 @@
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>

@ -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<Parameter> 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();
}
}

@ -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;
}
}

@ -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";
}
}

@ -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;
}

@ -0,0 +1,5 @@
#spring:
# profiles:
# active: test # 当前环境为dev
server:
port: 8080
Loading…
Cancel
Save