<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.example.hello.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
//创建人
.contact(new Contact("cookie", "http://www.baidu.com", "2118119173@qq.com"))
//版本号
.version("1.0")
//描述
.description("用户管理")
.build();
}
}
http://localhost:8080/swagger-ui.html
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
同swagger2
http://localhost:8080/swagger-ui/index.html
@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个对象参数描述
@ApiResponse(code = 200, message = "成功,返回用户信息",response = Dept.class,responseContainer = "List")
@ApiParam(value = "用户信息")
@PostMapping("/getDeptListByPage")
public MyResponse<List<Dept>> selectList( @RequestBody Dept param) {
List<Dept> depts = deptService.selectList(param);
return MyResponse.success(depts);
}
@ApiModel:用对象来接收参数
@ApiModelProperty
@ApiModel("部门实体类")
public class Dept extends BasicEntity {
/**
* 部门编号
*/
@ApiModelProperty(value = "部门编号",example = "100")
private Integer deptno;
/**
* 部门名称
*/
@ApiModelProperty("部门名称")
private String dname;
/**
* 部门所在地
*/
@ApiModelProperty("部门所在地")
private String loc;
}
@ApiResponse:HTTP响应其中1个描述
@ApiResponse(code = 200, message = "成功,返回用户信息",response = Dept.class,responseContainer = "List")
@ApiParam(value = "用户信息")
@PostMapping("/getDeptListByPage")
public MyResponse<List<Dept>> selectList( @RequestBody Dept param) {
List<Dept> depts = deptService.selectList(param);
return MyResponse.success(depts);
}
@ApiParamImplicitL:一个请求参数 @ApiParamsImplicit 多个请求参数
@ApiOperation("根据地址和名字查询部门")
@ApiImplicitParams({
@ApiImplicitParam(name = "dname", value = "部门名称", example = "开发部"),
@ApiImplicitParam(name = "loc", value = "部门地址", example = "北京")
})
@DeleteMapping("/getDept")
public MyResponse getDept(String dname, String loc) {
return MyResponse.success();
}
基本类型用@ApiParam不生效??
@ApiOperation("根据主键删除")
@ApiParam(name = "id", value = "部门编号", example = "100")
// @ApiImplicitParam(name = "id", value = "部门编号", example = "100")
@DeleteMapping("/dept/{id}")
public MyResponse deleteByPrimaryKey(@PathVariable("id") Integer id) {
deptService.deleteByPrimaryKey(id);
return MyResponse.success().setMessage("删除成功");
}
@ApiOperation("根据主键删除")
@ApiParam(name = "id", value = "部门编号", example = "100")
// @ApiImplicitParam(name = "id", value = "部门编号", example = "100")
@DeleteMapping("/dept2")
public MyResponse deleteByPrimaryKey2(Integer id) {
deptService.deleteByPrimaryKey(id);
return MyResponse.success().setMessage("删除成功");
}