spring cloud-Swagger2整合Eureka,实现Eureka页面自动跳转到Swagger2UI界面查看服务接口信息
2017-12-08 15:24
1101 查看
1.swagger官网:http://swagger.io/
2.github使用例子: https://github.com/swagger-api/swagger-codegen/blob/master/samples/server/petstore/springboot/src/main/java/io/swagger/api/PetApi.java
接入的第一步是加入依赖:
然后创建一个配置类SwaggerConfig
然后就可以在项目中使用swagger了
下面是swagger各个注解的参数说明
下面是swagger真实的使用demo例子
启动eureka,同时启动main方法然后访问
http://localhost:2222/swagger-ui.html#!/端口是你项目设置的端口,其他的相信你会懂的
以上要求你必须要有个eureka,具体的可参看相关文档
具体实现可以看我的github项目 https://github.com/nullPointException1/swagger-example-lsh 我的demo eureka配置好了的
2.github使用例子: https://github.com/swagger-api/swagger-codegen/blob/master/samples/server/petstore/springboot/src/main/java/io/swagger/api/PetApi.java
接入的第一步是加入依赖:
<!-- api 文档--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version > </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
然后创建一个配置类SwaggerConfig
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { ;return new Docket(DocumentationType.SWAGGER_2).apiInfo(b2BApiInfo()).select() .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot"))) .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud"))) .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc"))) .build(); } /** * 接口介绍 * @return */ private ApiInfo b2BApiInfo() { return new ApiInfoBuilder() //大标题 .title("B2B接口文档") //详细描述 .description("B2B的接口文档说明") .build(); } }
然后就可以在项目中使用swagger了
下面是swagger各个注解的参数说明
/** @Api:用在类上,说明该类的作用 @ApiOperation:用在方法上,说明方法的作用 @ApiImplicitParams:用在方法上包含一组参数说明 @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面 paramType:参数放在哪个地方 header-->请求参数的获取:@RequestHeader query-->请求参数的获取:@RequestParam path(用于restful接口)-->请求参数的获取:@PathVariable body(不常用) form(不常用) name:参数名 dataType:参数类型 required:参数是否必须传 value:参数的意思 defaultValue:参数的默认值 @ApiResponses:用于表示一组响应 @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息 code:数字,例如400 message:信息,例如"请求参数没填好" response:抛出异常的类 @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候) @ApiModelProperty:描述一个model的属性 */
下面是swagger真实的使用demo例子
@Api(value = "测试swagger Control", description = "测试swagger Control") @RestController @CrossOrigin @RequestMapping("/b2bTest") public class B2bTestControl { //当前数据类型 private final String APP_TYPE = "b2bMall"; @Reference(version = "1.0.0", timeout = 3000) private MallAppContentInfoService mallAppContentInfoService; @Autowired private B2bNoticePublishService b2bNoticePublishService; @ApiOperation(value="获取帮助内容详细信息", notes="根据url的id来获取帮助内容",consumes="application/x-www-form-urlencoded") @GetMapping("view") @ApiResponses({ @ApiResponse(code=400,message="请求参数没填好"), @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对") }) public MallAppContentInfoVo getInfo(@ApiParam(value = "ID值",defaultValue = "10")@RequestParam(value = "id") Long id) { // 处理"/b2bContentInfo/{id}"的GET请求,用来获取url中id值的帮助内容r信息 // url中的id可通过@PathVariable绑定到函数的参数中 MallAppContentInfoVo b2bContentInfo = mallAppContentInfoService.getById(id); return b2bContentInfo; } @ApiOperation(value="获取公告", notes="根据url的id来获取帮助内容") @GetMapping("list") @ApiResponses({ @ApiResponse(code=400,message="请求参数没填好"), @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对") }) public List<B2bNoticePublish> list(@ApiParam(value = "B2bNoticePublishQuery对象")B2bNoticePublishQuery query) { List<B2bNoticePublish> list = b2bNoticePublishService.getListByPage(query); return list; } }
启动eureka,同时启动main方法然后访问
http://localhost:2222/swagger-ui.html#!/端口是你项目设置的端口,其他的相信你会懂的
以上要求你必须要有个eureka,具体的可参看相关文档
具体实现可以看我的github项目 https://github.com/nullPointException1/swagger-example-lsh 我的demo eureka配置好了的
相关文章推荐
- spring cloud-Swagger2整合Eureka,实现Eureka页面自动跳转到Swagger2UI界面查看服务接口信息
- spring cloud-Swagger2整合Eureka,实现Eureka页面自动跳转到Swagger2UI界面查看服务接口信息
- 基于spring-cloud相关技术整合,实现接口调用、服务容错、动态路由配置等
- spring cloud-整合Swagger2构建RESTful服务的APIs
- spring cloud-整合Swagger2构建RESTful服务的APIs
- Spring Cloud eureka 注册服务发现,访问管理页面是xml问题解决
- 使用Spring Cloud微服务框架进行多个微服务整合时出现No converter found for return value of type:xxx异常信息
- Spring-cloud & Netflix 源码解析:Eureka 服务注册发现接口 ****
- Spring Cloud 实现微服务在Eureka的手动上下线
- 微服务实战之春云与刀客(二)—— Spring cloud 实现仿RPC面向接口调用方式
- 整合spring cloud云服务架构 - eureka 基础
- Asp.net实现网站提示信息页面后的定时自动跳转到指定页面
- spring cloud-整合Swagger2构建RESTful服务的APIs
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
- (九)整合spring cloud云服务架构 - HongHu commonservice-eureka 项目构建过程
- (二)SpringBoot+SpringCloud —— 使用Eureka实现服务注册与发现
- 基于spring-cloud实现eureka注册服务小案例
- Spring Cloud Eureka 实现服务注册与发现
- SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端、Eureka 服务信息、Eureka 发现管理、Eureka 安全配置、Eureka-HA(高可用) 机制、Eureka 服务打包部署)
- (八)整合spring cloud云服务架构 - eureka 基础