接口在线文档swagger-UI整合
1.依赖映入
<!--swagger-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
2.新增swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("liut", "", "1299828500@qq.com"))
.build();
}
}
3.在Controller类上增加swagger配置
@SpringBootApplication
@EnableFeignClients
public class ZdxsApplication {
public static void main(String[] args) {
SpringApplication.run(ZdxsApplication.class, args);
}
}
4.在Controller加入ApiOperation注释
@RequestMapping(value="getRxtdGet",method=RequestMethod.GET)
@ApiOperation(value = "根据当前页数直接从系统查询当日人行通道数据", notes="get方式")
public JSONObject getRxtdGet(HttpServletRequest request,
@ApiParam(value = "页数/每页显示100条", required = true) @RequestParam(value="pageNo",required=true)Integer pageNo){
}
以上配置完成之后,直接启动项目,访问地址:localhost:8080/swagger-ui.html,即可打开如下页面(自己yml设置的项目端口号)
但是原生的swagger-UI不是很友好,现在swagger-bootstrap-ui,这是国人开发的ui包,感觉非常不错。
1.引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2.新增swagger配置类
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("接口文档")
.build();
}
4.在Controller加入ApiOperation注释
@Api(value="/test1", tags="测试接口模块")
@RestController
public class testContro {
@ApiOperation("test")
@GetMapping("/ha")
public String test(){
return "test成功";
}
@ApiOperation("Area删除")
@GetMapping("/a")
public String ha(){
return "Area删除成功";
}
}
访问地址:localhost:8080/doc.html