springboot 简单使用swagger生成接口文档。
1.pom添加依赖
<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>
2.@ApiOperation,@ApiImplicitParams接口说明注解
@ApiOperation(value="修改用户",notes="对用户进行更新操作")
@RequestMapping(value="/{id}",method = RequestMethod.PUT)
@ApiImplicitParams({
@ApiImplicitParam(name="id",value="用户id",dataType="long"),
@ApiImplicitParam(name="user2",value="用户实体",dataType="User2")})
public User2 updateUser(@PathVariable Long id, @RequestBody User2 user2){
User2 user = new User2();
user.setId(id);
user.setUserName(user2.getUserName());
user.setPassword(user2.getPassword());
return userService.updateUser(user);
}
@ApiOperation(value="删除用户",notes = "根据用户id进行删除操作")
@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
public void deleteUser(@PathVariable Long id){
userService.deleteUser(id);
}
3.Swagger配置类,注意得写正确接口所在包名
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.demo2"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("练习restful api文档")
.description("简单优雅的restfun风格")
.version("1.0")
.build();
}
}
4.启动程序,输入网址 http://localhost:8077/swagger-ui.html