在Spring Boot工程的pom文件中引入swagger2的依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
编写swagger2的配置文件
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//com.person.demo 项目的包名
.apis(RequestHandlerSelectors.basePackage(("com.person.demo")))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restful风格")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
使用swagger2示例,通过@ApiOperation注解描述生成在线文档的具体API说明,其中value值为该接口的名称,notes值为该接口的详细文档说明。如果不需要某个接口生成文档,只需要再加上@ApiIgnore注解即可。启动工程,在浏览器中访问http://localhost:8080/swagger-ui.html 浏览器就会显示在线文档的界面了(8080为工程启动的端口号)
@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ApiOperation(value = "登录",notes = "用户登录接口")
public ResultVo login(@RequestParam("account") String account, @RequestParam("password") String password ){
return loginService.login(account,password);
@RequestMapping(value = "/getPhoto",method = RequestMethod.GET)
@ApiIgnore //使用该注解忽略这个API
public ResultVo getPhoto(String account){
return loginService.getPhoto(account);
}
}
}