文章出處
View Code
View Code
View Code
文章列表
對絕大多數程序員而言,寫接口文檔是一件痛苦的事情,相對文檔,他們更愿意寫代碼。最理想的情況就是:代碼即文檔!服務開發完成后,部署上去文檔就自動生成,沒錯,這就是springfox + swagger要解決的問題!
swagger號稱 THE WORLD'S MOST POPULAR API TOOLING。但swagger默認情況下,仍要單獨部署,程序員還是要跑到一個單獨的web頁面上編輯,寫一堆yaml文檔,依然不爽。
github上有一個springfox項目,可以在開發rest服務時,只要加一些注解,就自動生成swagger-ui界面,以及相關的文檔,而且可以跟spring-boot/spring-cloud無縫集成。
步驟:
一、添加依賴項
dependencies { ... compile "io.springfox:springfox-swagger2:2.7.0" compile "io.springfox:springfox-swagger-ui:2.7.0" ... }
二、添加swagger配置類

1 package cn.mwee.order.cloud.service.express.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 7 import springfox.documentation.builders.ApiInfoBuilder; 8 import springfox.documentation.builders.PathSelectors; 9 import springfox.documentation.builders.RequestHandlerSelectors; 10 import springfox.documentation.service.ApiInfo; 11 import springfox.documentation.service.Contact; 12 import springfox.documentation.spi.DocumentationType; 13 import springfox.documentation.spring.web.plugins.Docket; 14 import springfox.documentation.swagger2.annotations.EnableSwagger2; 15 16 /** 17 * Created by yangjunming on 13/10/2017. 18 */ 19 @Configuration 20 @EnableSwagger2 21 public class SwaggerConfig { 22 23 @Bean 24 public Docket createRestApi() { 25 return new Docket(DocumentationType.SWAGGER_2) 26 .apiInfo(apiInfo()) 27 .select() 28 .apis(RequestHandlerSelectors.basePackage("xxx.controller")) //Controller所在的package 29 .paths(PathSelectors.any()) 30 .build(); 31 } 32 33 @Bean 34 public WebMvcConfigurerAdapter addResourceHandlers() { 35 return new WebMvcConfigurerAdapter() { 36 @Override 37 public void addResourceHandlers(ResourceHandlerRegistry registry) { 38 registry.addResourceHandler("swagger-ui.html") 39 .addResourceLocations("classpath:/META-INF/resources/"); 40 registry.addResourceHandler("/webjars/**") 41 .addResourceLocations("classpath:/META-INF/resources/webjars/"); 42 } 43 }; 44 } 45 46 private ApiInfo apiInfo() { 47 return new ApiInfoBuilder() 48 .title("express-service online api document") 49 .description("某某服務") 50 .contact(new Contact("作者名字", "http://作者介紹網址", "作者@郵箱.com")) 51 .version("1.0.0") //顯示版本號 52 .build(); 53 } 54 }
三、在Controller上添加注解

1 package cn.mwee.order.cloud.service.express.controller; 2 3 import cn.mwee.order.cloud.admin.common.controller.AbstractController; 4 import cn.mwee.order.cloud.admin.service.dto.common.DataResult; 5 import cn.mwee.order.cloud.admin.service.dto.express.sf.*; 6 import cn.mwee.order.cloud.admin.service.express.SFExpressService; 7 import io.swagger.annotations.Api; 8 import io.swagger.annotations.ApiOperation; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.cloud.context.config.annotation.RefreshScope; 11 import org.springframework.web.bind.annotation.PostMapping; 12 import org.springframework.web.bind.annotation.RequestBody; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.bind.annotation.RestController; 15 16 import javax.servlet.http.HttpServletRequest; 17 18 /** 19 * Created by 菩提樹下的楊過(http://yjmyzz.cnblogs.com/) on 2017/7/18. 20 */ 21 @RestController 22 @RefreshScope 23 @RequestMapping("/sf") 24 @Api(consumes = "application/json", 25 produces = "application/json", 26 protocols = "http", 27 basePath = "/sf", value = "順豐對接服務") 28 public class SFController extends AbstractController { 29 30 ... 31 32 @ApiOperation(value = "下單", tags = "順豐速運") 33 @PostMapping(value = "submitOrder") 34 public DataResult<SFSubmitResponse> submitOrder(@RequestBody SFSubmitOrderRequest request) throws Exception { 35 return ...; 36 } 37 38 ... 39 40 }
注意上面幾處帶@Api開頭的注解
四、參數實體上添加注解

1 package cn.mwee.order.cloud.admin.service.dto.express.sf; 2 3 import cn.mwee.order.cloud.admin.service.dto.express.ExpressBaseOrder; 4 import cn.mwee.order.common.utils.date.DateUtil; 5 import com.alibaba.fastjson.annotation.JSONField; 6 import io.swagger.annotations.ApiModel; 7 import io.swagger.annotations.ApiModelProperty; 8 import lombok.Data; 9 import lombok.EqualsAndHashCode; 10 11 import java.util.ArrayList; 12 import java.util.Date; 13 import java.util.List; 14 15 /** 16 * 順豐的物流訂單 17 * Created by 菩提樹下的楊過(http://yjmyzz.cnblogs.com/) on 26/09/2017. 18 */ 19 @Data 20 @EqualsAndHashCode(callSuper = false) 21 @ApiModel(description = "順豐-提交訂單參數") 22 public class SFSubmitOrderRequest extends ExpressBaseOrder { 23 24 @ApiModelProperty(value = "商家編碼(順豐分配提供)", required = true, dataType = "String", example = "A001") 25 private String clientCode; //商家編碼,為順豐分配提供 26 27 ... 28 }
還是注意上面幾處帶@Api開頭的注解,一看就懂。
啟動后,就可以用 http://xxxx.xxxx:port/swagger-ui.html訪問了
隨便點開一個方法,比如cancelOrder
還可以直接調用REST(相當于postman之類的rest服務測試工具了)
文章列表
全站熱搜