1,导入依赖
<!--swagger-->
<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>
2,配置swagger【你可以新建一个swagger专用的配置类,也可以自己在applicant里配置】
直接在applicant里配置
/**
* swagger配置
* localhost:8080/v2/api-docs
* localhost:8080/swagger-ui.html
*
* @return
*/
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs,献给伟大的程序员")
.description("纯棉生活小程序api")
.termsOfServiceUrl("www.baidu.com")
.version("1.0").build();
}
3,开始使用吧,写一个controller
@Api(description = "【用户接口】登录,修改个人信息")
@RestController
public class LoginController extends BaseController {
@ApiOperation(value = "用户登录")
@RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
public ResultSet userLogin(HttpServletRequest request,
@ApiParam(name = "mobile", value = "手机号码", required = true) @RequestParam String mobile,
@ApiParam(name = "code", value = "验证码", required = true) @RequestParam String code
) {
return ResultSet.createSuccess("登录成功!");
}
}
好了
看一下运行的样子吧 【http://localhost:9090/swagger-ui.html】