SSM框架基于swagger2实现API管理
2017-11-14 14:21
477 查看
SSM框架基于swagger实现API管理
swagger官网:https://swagger.io/docs/
1.添加maven依赖
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swaggger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swaggger.version}</version> </dependency>
2.下载swagger
https://swagger.io/download-swagger-ui/3.配置SwaggerConfig
/** * @Author: CatalpaFlat * @Descrition: * @Date: Create in 11:26 2017/11/14 * @Modified BY: */ @EnableWebMvc @EnableSwagger2 @Configuration @ComponentScan(basePackages ="com.chen") public class SwaggerConfig extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.chen")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SSM-Swagger2 APIs") .termsOfServiceUrl("http://blog.csdn.net/dushiwodecuo") .contact(new Contact("CatalpaFlat","http://blog.csdn.net/dushiwodecuo","1013427541@qq.com")) .version("1.0.0") .build(); } }
4.修改swagger2附带的index.html
修改index.html中的 http://petstore.swagger.wordnik.com/v2/swagger.json修改为自己项目路径+api-docs,例如:http://localhost:8080/api/api-docs
5.注解使用
@RequestMapping @RestController @Api(description="测试接口") public class TestController { @Resource private TestService testService; @GetMapping(value = "get") @ApiOperation(value = "获取值",httpMethod = HttpMethod.GET,response = List.class,notes ="get name") public List<Test> get(){ return testService.get(); } @ApiOperation(value = "设置值",httpMethod = HttpMethod.GET,response = String.class,notes ="set name") @ApiImplicitParam(value = "name",dataType = "form",required = true,defaultValue = "21378127") @GetMapping(value = "set/{name}",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public String set(@PathVariable String name){ return testService.set(name); } }
5.启动测试
浏览器地址栏输入:http://localhost:8080/api/index.html6.swagger2常用注解如下
@Api()用于类;表示标识这个类是swagger的资源
@ApiOperation()用于方法;
表示一个http请求的操作
@ApiParam()用于方法,参数,字段说明;
表示对参数的添加元数据(说明或是否必填等)
@ApiModel()用于类
表示对类进行说明,用于参数用实体类接收
@ApiModelProperty()用于方法,字段
表示对model属性的说明或者数据操作更改
@ApiIgnore()用于类,方法,方法参数
表示这个方法或者类被忽略
@ApiImplicitParam() 用于方法
表示单独的请求参数
@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
相关文章推荐
- 基于Oracle数据库,用SSM框架和easyUI实现分页查询操作
- SSM框架 基于spring-AOP 实现requet参数日志记录
- 23、SSM框架-Mybatis基于注解实现增删查改和多参数列表查询(5)
- 基于SSM框架学生管理系统的实现
- SSM框架-Mybatis基于注解实现增删查改和多参数列表查询
- 基于SSM框架实现数据库的基本功能(一)
- 基于SSM框架web搜索功能的实现
- 基于ssm框架的个人博客(8)--Blog相关数据库操作实现
- 基于SSM框架实现基本文件操作
- 基于SSM框架实现EXCEL文件的上传下载
- 使用Maven基于ssm框架的完整商务管理开发项目(01)
- 基于SSM框架的Maven项目的搭建
- [置顶] Spring4.0.9+SpringMVC4.0.9+SpringSecurity3.2.5+MyBatis3.2.5+Activiti5.21即:SSM框架的企业级搭建之番外篇— 根据POI技术实现
- 高层游戏引擎——基于OGRE所实现的高层游戏引擎框架
- ssh/ssm使用CXF实现基于Rest方式的WebService并返回json数据
- 使用poi实现java读取excel表格并存储到数据库中解决读取日期格式问题(ssm框架)
- 基于AJAX技术实现Struts校验框架
- SSM框架——以注解形式实现事务管理
- 基于Metronic的Bootstrap开发框架经验总结(17)-- 使用 summernote插件实现HTML文档的编辑和图片插入操作
- Dubbo框架应用之(四)--Dubbo基于Zookeeper实现分布式实例