现在的项目大部分都是前后端分离的,联调的时候沟通成本是个问题,而Swagger能很好地减少沟通成本,下面讲解Spring Boot如何集成Swagger。
Maven:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
新建Swagger2文件(跟启动类在同一个目录):
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createdRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试API")
.contact(new Contact("manonglixianji", "", ""))
.version("1.0")
.description("测试接口")
.build();
}
}
接着在Controller层的方法中集成Swagger就行了:
@RequestMapping("/test")
@RestController
@Api("码农历险记api")
public class testController {
@Autowired
private TestService testService;
/**
* 测试列表
* @param date
* @return
*/
@GetMapping("/page")
@ApiOperation(value = "分页查询列表",notes = "分页查询操作")
@ApiImplicitParams({ @ApiImplicitParam(value = "页数",name = "page",dataType = "int",paramType = "path"),
@ApiImplicitParam(value = "页数大小",name = "size",dataType = "int",paramType = "path"),
@ApiImplicitParam(value = "日期",name = "date",dataType = "string",paramType = "path")})
public Page<ReturnVO> findDormCodeList(@PageableDefault Pageable pageable,
@RequestParam(value = "date",required = false) String date) {
return testService.findTestList(pageable,date);
}
}
实体类ReturnVO:
最后启动我们的项目在浏览器输入:http://localhost:8080/swagger-ui.html 结果就出来了。
来源:CSDN
作者:码农历险技
链接:https://blog.csdn.net/longxudong223/article/details/103455048