spring-boot整合swagger生成在线api文档
2018-02-21 18:13
1231 查看
参考链接:https://www.cnblogs.com/luoluocaihong/p/7106276.html
最近用springboot构建rest接口,考虑到最方便的验证接口,想到了引入swagger。
基本的步骤大致如下:
1.pom中引入swagger依赖:
2.创建swagger的配置类:
然后在controller层的方法中添加注解
比如这样:
然后访问http://localhost:8080/swagger-ui.html这个路径。
中间可能会出现json的错误,就是你项目中采用的是fastjson,出现错误提示
解决方法自行百度把。
最近用springboot构建rest接口,考虑到最方便的验证接口,想到了引入swagger。
基本的步骤大致如下:
1.pom中引入swagger依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency>
2.创建swagger的配置类:
package com.coder520.mamabike.common.swagger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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 SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.coder520.mamabike")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("码码共享单车API文档") .version("1.0") .build(); } }
然后在controller层的方法中添加注解
比如这样:
@ApiOperation(value="短信验证码",notes = "根据用户手机号码发送验证码",httpMethod = "POST") @ApiImplicitParam(name = "user",value = "用户信息 包含手机号码",required = true,dataType = "User") @RequestMapping("/sendVercode") public ApiResult sendVercode(@RequestBody User user,HttpServletRequest request){ ApiResult<String> resp = new ApiResult<>(); try { if(StringUtils.isEmpty(user.getMobile())){ throw new MaMaBikeException("手机号码不能为空"); } userService.sendVercode(user.getMobile(),getIpFromRequest(request)); resp.setMessage("发送成功"); } catch (MaMaBikeException e) { resp.setCode(e.getStatusCode()); resp.setMessage(e.getMessage()); } catch (Exception e) { log.error("Fail to update user info", e); resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR); resp.setMessage("内部错误"); } return resp; }
然后访问http://localhost:8080/swagger-ui.html这个路径。
中间可能会出现json的错误,就是你项目中采用的是fastjson,出现错误提示
fetching resource list: http://localhost:8080/v2/api-docs; Please wait.
解决方法自行百度把。
相关文章推荐
- Springboot | SpringBoot 微服务整合Swagger生成API文档
- 使用 SpringBoot + Swagger 生成接口 API 文档
- spring boot利用swagger和spring doc生成在线和离线文档
- Swagger自动接口文档生成框架————springboot整合swagger总结
- Swagger2 生成 Spring Boot API 文档
- SpringBoot&Swagger构建REST API并生成API文档
- SpringBoot + mybatis + Swagger快速构建REST API并生成优美的API文档
- SpringBoot:番外篇SpringBoot+Swagger生成可视图的API文档
- springboot 集成 swagger生成在线文档
- Spring Boot如何让Web API自动生成文档,并解决swagger-annotations的API注解description属性废弃的问题
- 使用Spring Boot&Swagger快速构建REST API并生成优美的API文档
- Spring boot结合swagger自动生成api文档
- SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)
- [置顶] spring boot项目实战:swagger2在线文档
- SpringBoot整合Swagger自动生成API文档
- [置顶] spring boot项目实战:swagger2在线文档
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
- SpringBoot实践之---集成Swagger2生成Restful风格的在线API
- [置顶] spring boot项目实战:swagger2在线文档
- Swagger2 生成 Spring Boot API 文档