1.swagger官网:http://swagger.io/
接入的第一步是加入依赖:
<!-- 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配置好了的