swagger是一个很好用的工具,可以帮我们生成我们的开发文档,下面详细介绍我们的项目如何集成一个swagger插件
1、首先添加我们的项目依赖
<dependency><groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、增加wwagger的配置
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
//为当前包下controller生成API文档
.apis(RequestHandlerSelectors.basePackage("com.example.datareduction.controller"))
//为有@Api注解的Controller生成API文档
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//为有@ApiOperation注解的方法生成API文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger")
.description("Swagger Test")
.contact("xiaoyong.ke")
.version("1.0")
.build();
}
}
3、增加玩配置文件之后,我们测试一下,在我们的控制层方法增加注解,以下是各注解使用说明
@Api(tags = “swagger 测试”) 对类的请求说明
@ApiOperation 方法的说明
@ApiImplicitParams 方法参数的说明;
@ApiImplicitParam 用于指定单个参数的说明
@ApiResponses 方法返回值的说明 ;
@ApiResponse 用于指定单个参数的说明
@ApiModel 用在JavaBean类上,说明JavaBean的 整体用途
@ApiModelProperty 用在JavaBean类的属性上面,说明此属性的的含议
示例:
@RestController
//@RequestMapping("/test")
@Api(tags = "swagger 测试")
public class SwaggerTest {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping(value = "/test",method = RequestMethod.GET)
@ApiOperation("测试")
public String test(String test){
System.out.println("测试wwagger");
return test;
}
}
4、最后访问我们的swagger地址 http://localhost:8888/swagger-ui.html
这里端口号按照你们自己的来就可以看到啦