swagger2 API文档的框架 配置
2018-02-20 20:51
363 查看
随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。
前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,
pom.xml 添加依赖
<!-- Restful API接口说明 生成 swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>controller内容
@RequestMapping(value = "/per/{id}", method = RequestMethod.GET)
@ApiOperation(value = "测试", notes = "测试逆向工程")
public ResultVO selectByPrimaryKey(@PathVariable("id")Integer id) {
per perLIst= userservice.selectByPrimaryKey(id);
return ResultVOUtil.success(perLIst);
}
swagger2文件内容
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("文档")
.description("提供给外部系统的服务接口,进行管理业务。")
.termsOfServiceUrl("http://terms-of-services.url")
.version("1.0")
.build();
}
}访问地址:http://localhost:8085/swagger-ui.html
可以测试很方便 点Try it out!
我的demo用的事JSON传数据 用的RESTful风格 get 查询 post 创建 put 更新 del 删除 都可以测试
前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,
swagger就是一款让你更好的书写API文档的框架。
pom.xml 添加依赖
<!-- Restful API接口说明 生成 swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>controller内容
@RequestMapping(value = "/per/{id}", method = RequestMethod.GET)
@ApiOperation(value = "测试", notes = "测试逆向工程")
public ResultVO selectByPrimaryKey(@PathVariable("id")Integer id) {
per perLIst= userservice.selectByPrimaryKey(id);
return ResultVOUtil.success(perLIst);
}
swagger2文件内容
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("文档")
.description("提供给外部系统的服务接口,进行管理业务。")
.termsOfServiceUrl("http://terms-of-services.url")
.version("1.0")
.build();
}
}访问地址:http://localhost:8085/swagger-ui.html
可以测试很方便 点Try it out!
我的demo用的事JSON传数据 用的RESTful风格 get 查询 post 创建 put 更新 del 删除 都可以测试
相关文章推荐
- swagger:API在线文档自动生成框架
- API文档自动生成,Swagger的配置
- 开源的API文档工具框架——Swagger简介
- Hibernate框架(一)文件配置、核心API、对象状态详解
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
- SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)
- SpringBoot项目使用Swagger生成api文档
- swagger生成API文档
- jersey+swagger,构建api文档平台
- 【Dubbo分布式服务框架】4.基于API配置的服务提供者和消费者
- Swagger RESTful API文档规范
- Swagger生成API文档
- Prototype功能预览十一:集成Swagger生成API文档及API测试界面
- Spring 4 官方文档学习(十一)Web MVC 框架之约定优于配置
- Myeclipse配置Struts显示jar包源码以及Api文档
- java服务器使用swagger自动生成API文档
- spring集成swagger生成api文档的丢弃接口添加删除线效果
- 浅析如何在Nancy中使用Swagger生成API文档
- iOS自带框架以及API文档
- swagger生成API文档