版权声明:YETA https://blog.csdn.net/qq_28958301/article/details/84842547
一、依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
二、配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //创建api的基本信息
.select() //控制哪些接口暴露给Swagger来展现
.apis(RequestHandlerSelectors.basePackage("com.yeta.pps.test")) //采用指定扫描的包路径来定义
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("这是标题")
.description("这是描述")
.termsOfServiceUrl("这是url")
.contact("这是联系人")
.version("这是版本")
.build();
}
}
三、测试接口
@Api(value = "测试相关接口")
@RestController
@RequestMapping(value = "/test")
public class TestController {
static class People {
private Integer id;
private String name;
public People() {
}
public People(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
static class CommonResponse<T> {
private Integer code;
private T result;
public CommonResponse() {
}
public CommonResponse(Integer code, T result) {
this.code = code;
this.result = result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
@Override
public String toString() {
return "CommonResponse{" +
"code=" + code +
", result=" + result +
'}';
}
}
/*@ApiOperation(value = "新增")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "int"),
@ApiImplicitParam(name = "name", value = "name", required = true, paramType = "query", dataType = "String")
})
@PostMapping(value = "/peoples")
public CommonResponse<People> add(@RequestParam(value = "id") Integer id,
@RequestParam(value = "name") String name) {
return null;
}*/
@ApiOperation(value = "新增")
@ApiImplicitParam(name = "people", value = "people", required = true, paramType = "body", dataType = "People")
@PostMapping(value = "/people")
public CommonResponse<People> add(@RequestBody People people) {
return null;
}
@ApiOperation(value = "删除")
@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "int")
@DeleteMapping(value = "/peoples/{id}")
public CommonResponse<People> delete(@PathVariable(value = "id") Integer id) {
return null;
}
@ApiOperation(value = "修改")
@ApiImplicitParam(name = "people", value = "people", required = true, paramType = "body", dataType = "People")
@PutMapping(value = "/people")
public CommonResponse<People> update(@RequestBody People people) {
return null;
}
@ApiOperation(value = "查找全部")
@GetMapping(value = "/peoples")
public CommonResponse<People> findAll() {
return null;
}
/*@ApiOperation(value = "根据id获取")
@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "int")
@GetMapping(value = "/peoples/id")
public CommonResponse<People> findById(@RequestParam(value = "id") Integer id) {
return null;
}*/
@ApiOperation(value = "根据id获取")
@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "int")
@GetMapping(value = "/peoples/{id}")
public CommonResponse<People> findById(@PathVariable(value = "id") Integer id) {
return null;
}
}
四、地址
http://localhost:8080/swagger-ui.html
五、效果