SpringBoot_Swagger
第一步
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
第二步
@Configuration
@EnableOpenApi
@EnableKnife4j
public class SwaggerConfig {
@Bean
public Docket openApi() {
return new Docket(DocumentationType.OAS_30)
.groupName("Test Group")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger API")
.description("Test Api")
.contact(new Contact("test", "http://swagger.com", "swagger@gmail.com"))
.version("1.0")
.build();
}
}
第三步
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "用户实体类")
public class UserParam implements Serializable {
@ApiModelProperty(value = "ID")
private Integer id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "性别")
private String sex;
}
第四步
@Api(value = "UserController")
@RestController
@RequestMapping("/user")
public class UserController {
@PostMapping("/add")
@ApiOperation("新增方法")
@ApiImplicitParam(name = "userParam", type = "Body", dataTypeClass = UserParam.class, required = true)
public Boolean add(@RequestBody UserParam userParam) {
return true;
}
@PostMapping("/list")
@ApiOperation("查询方法")
public List<UserParam> getList() {
List<UserParam> list = new ArrayList<>();
UserParam userParam = new UserParam(1, "ka", "男");
list.add(userParam);
return list;
}
}