jeesite使用swagger管理api接口
2018-04-02 11:05
579 查看
项目开发协作时,api接口文档写起来很麻烦,现在也有许多工具能做到自动生成api文档的效果,这里介绍中笔者在开发中使用过的工具:swagger,需要的同学请拿走。
闲话少说,上干货:
maven配置:
<!-- Swagger API tools -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.5.0</version>
</dependency>
SwaggerConfig :
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages ={"com.thinkgem.jeesite.dida"}) //接口所在的包路径
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("x-access-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.thinkgem.jeesite.dida"))//接口所在的包路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("**项目接口文档:")
.termsOfServiceUrl("http://localhost:8080/flydi")
.contact("**项目组")
.version("1.1.1")
.build();
}
}
接口示例:
@Controller
@Api(value="AppLoginController",tags={"app登陆接口"})
public class AppLoginController extends BaseController {
@Autowired
private AppUserService appUserService;
@ApiImplicitParams({
@ApiImplicitParam(name = "phoneNo", paramType="query" ,value = "手机", required = true, dataType = "string"),
@ApiImplicitParam(name = "password", paramType="query" , value = "密码", required = true, dataType = "string")
})
@RequestMapping(value = "${didaPath}/applogin", method = RequestMethod.POST)
public String registered(HttpServletRequest request,HttpServletResponse response) {
String phoneNo = WebUtils.getCleanParam(request, "phoneNo");
String password = WebUtils.getCleanParam(request, "password");
AppUser user = new AppUser();
user.setPhoneNo(phoneNo);
user.setPassword(password);
int i = appUserService.insertUser(user);
ResponseBody re=new ResponseBody();
re.setData(i);
return renderString(response, re);
}
}
访问路径:http://127.0.0.1:8080/flydi/swagger-ui.html
效果图:
测试结果:
有些接口不需要显示的添加注解:
@ApiIgnore
第一次需要maven install 之后才会显示接口。
闲话少说,上干货:
maven配置:
<!-- Swagger API tools -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.5.0</version>
</dependency>
SwaggerConfig :
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages ={"com.thinkgem.jeesite.dida"}) //接口所在的包路径
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("x-access-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.thinkgem.jeesite.dida"))//接口所在的包路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("**项目接口文档:")
.termsOfServiceUrl("http://localhost:8080/flydi")
.contact("**项目组")
.version("1.1.1")
.build();
}
}
接口示例:
@Controller
@Api(value="AppLoginController",tags={"app登陆接口"})
public class AppLoginController extends BaseController {
@Autowired
private AppUserService appUserService;
@ApiImplicitParams({
@ApiImplicitParam(name = "phoneNo", paramType="query" ,value = "手机", required = true, dataType = "string"),
@ApiImplicitParam(name = "password", paramType="query" , value = "密码", required = true, dataType = "string")
})
@RequestMapping(value = "${didaPath}/applogin", method = RequestMethod.POST)
public String registered(HttpServletRequest request,HttpServletResponse response) {
String phoneNo = WebUtils.getCleanParam(request, "phoneNo");
String password = WebUtils.getCleanParam(request, "password");
AppUser user = new AppUser();
user.setPhoneNo(phoneNo);
user.setPassword(password);
int i = appUserService.insertUser(user);
ResponseBody re=new ResponseBody();
re.setData(i);
return renderString(response, re);
}
}
访问路径:http://127.0.0.1:8080/flydi/swagger-ui.html
效果图:
测试结果:
有些接口不需要显示的添加注解:
@ApiIgnore
第一次需要maven install 之后才会显示接口。
相关文章推荐
- 使用 SpringBoot + Swagger 生成接口 API 文档
- Swagger UI教程 API 文档神器 搭配Node使用 web api 接口文档 (转)
- API接口在线管理(Swagger和SosoApi)
- 使用Swagger在SpringBoot项目中管理API文档(使用Oauth2)
- 在 IBM WebSphere Service Registry and Repository 中使用简单 XML 接口和 JMX管理 API分类加载
- 使用twisted创建管理ftp账号数据库的接口
- 如何使用 Lotus Notes C API 接口得到 DDM 的事件信息
- 验证使用出站接口配置的静态路由管理距离
- XPath API的使用 常用类及接口
- FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解
- FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解
- php飞信api接口[免费使用]
- 使用事务管理抽象API进行事务界定的代码示例
- 面向服务架构~面向服务的API是统一接口还是具体业务使用具体的接口?
- Vaadin Web应用开发教程(44): 使用Container接口管理一组Item
- 新浪微博API使用之python接口的使用
- curl c/c++ api接口使用例程
- IIS 7管理API——使用 Microsoft.Web.Administration
- curl c/c++ api接口使用例程 - Mr_Von的专栏 - 博客频道 - CSDN.NET
- 使用C#查询路由接口,同时小议一些.net 2.0的诡异API