集成swagger2构建Restful API的示例

  介绍

这篇文章给大家分享的是有关集成swagger2构建Restful API的示例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

<强>在pom。xml中进行版本管理

& lt; swagger.version> 2.8.0

<强>给taosir-api的砰的一声。xml中添加依赖配置

& lt; !——, swagger  start ——比;   & lt; dependency>   & lt;才能groupId> io.springfox</groupId>   & lt;才能artifactId> springfox-swagger2</artifactId>   & lt;才能version> $ {swagger.version} & lt;/version>   & lt;/dependency>   & lt; dependency>   & lt;才能groupId> io.springfox</groupId>   & lt;才能artifactId> springfox-swagger-ui</artifactId>   & lt;才能version> $ {swagger.version} & lt;/version>   & lt;/dependency>

<强>添加配置类

package  cn.taosir.api.config;      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;      @EnableSwagger2   @ configuration   public  class  SwaggerConfiguration  {   ,@ bean   public 才能;Docket  createRestApi (), {   ,,,return  new 摘要(DocumentationType.SWAGGER_2)   ,,,,,,,.apiInfo (apiInfo ())   ,,,,,,,.select ()   ,,,,,,,//控制暴露出去的路径下的实例   ,,,,,,,//如果某个接口不想暴露,可以使用以下注解   ,,,,,,,//@ApiIgnore 这样,该接口就不会暴露在,swagger2 的页面下   ,,,,,,,.apis (RequestHandlerSelectors.basePackage (“cn.taosir.api.controller"))   ,,,,,,,.paths (PathSelectors.any ())   ,,,,,,,.build ();   ,,}   private 才能;ApiInfo  apiInfo (), {   ,,,return  new  ApiInfoBuilder ()   ,,,,,,,.title(“涛先森系统入口业务测试“)   ,,,,,,,.version (“1.0”)   ,,,,,,,.description (“API 描述“)   ,,,,,,,.build ();   ,,}   }

<>强为控制层添加相应注解

package  cn.taosir.api.controller.dreamhouse;      import  org.springframework.beans.factory.annotation.Autowired;   import  org.springframework.web.bind.annotation.RequestMapping;   import  org.springframework.web.bind.annotation.RequestMethod;   import  org.springframework.web.bind.annotation.RestController;   import  cn.taosir.service.dreamHouse.UserService;   import  io.swagger.annotations.Api;   import  io.swagger.annotations.ApiOperation;   @RestController   @Api (value =,“用户管理“,,tags =,{“用户的接口“})   public  class  UserController  {   @ autowired才能   private 才能;UserService  userService;   ,,   @ApiOperation才能(值=https://www.yisu.com/zixun/辈馐苑椒ā?指出=安馐允欠癯晒κ褂梅穹⑾帧?   @RequestMapping (value="/测试”,方法=RequestMethod.GET)   公共字符串测试(){   返回userService.test ();   }   }

按顺序启动

taosir-eureka注册中心

taosir-dreamHouse服务提供者

taoisr-api服务消费者

访问地址http://localhost: 8765/swagger-ui。html #

集成swagger2构建Restful API的示例

集成swagger2构建Restful API的示例

以上,集成swagger2构建Restful API

下面附上注解参考表

@Api:用在请求的类上,表示对类的说明   标签才能=八得鞲美嗟淖饔?可以在UI界面上看到的注解“;   价值才能=https://www.yisu.com/zixun/备貌问皇裁匆庖?在UI界面上也看的到,所以不需要配置”      @ApiOperation:用在请求的方法上,说明方法的用途,作用   值="说明方法的用途,作用”   笔记="方法的备注说明”      @ApiImplicitParams:用在请求的方法上,表示一组参数说明   @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面   名称:参数名   价值:参数的汉字说明,解释   要求:参数是否必须传   paramType:参数放在哪个地方   ·头- ->请求参数的获取:@RequestHeader   ·查询- ->请求参数的获取:@RequestParam   ·路径(用于restful接口)——>请求参数的获取:@PathVariable   ·身体(不常用)   ·形式(不常用)   数据类型:参数类型,默认字符串,其它值数据类型=罢?   defaultValue:参数的默认值      @ApiResponses:用在请求的方法上,表示一组响应   @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息   代码:数字,例如400   信息:信息,例如“请求参数没填好“   回应:抛出异常的类      @ApiModel:用于响应类上,表示一个返回响应数据的信息   (这种一般用在文章创建的时候,使用@RequestBody这样的场景,   请求参数无法使用@ApiImplicitParam注解进行描述的时候)   null

集成swagger2构建Restful API的示例