API文檔的一種玩法
嵌入到java code中(YAPI是非嵌入式的)
項目啓動后,通過生成的link去訪問api,可以查看api使用方法、mock api等
Base on: spring boot
安裝
Add dependence in pom.xml
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>
Add config in application.properties:
#apispecProperties
hkbn.api.title=SiteMORuleAPI
hkbn.api.description=SiteMORuleAPI
hkbn.api.version=1.0
hkbn.switch=true
Add in weblogic.xml(主要是com.google):
<container-descriptor> <prefer-application-packages> <package-name>org.slf4j</package-name> <package-name>org.springframework.*</package-name> <package-name>com.fasterxml.jackson.*</package-name> <package-name>com.google.*</package-name> </prefer-application-packages> </container-descriptor>
Add class:
package hk.com.hkbn.itrbss; import org.springframework.beans.factory.annotation.Value; 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 ApiSpecConfig { @Value("${hkbn.api.title}") private String title; @Value("${hkbn.api.description}") private String description; @Value("${hkbn.api.version}") private String version; @Value("${hkbn.switch}") private boolean apiSpecSwitch; @Bean public Docket api() { Docket docket = new Docket(DocumentationType.SWAGGER_2); if (apiSpecSwitch) { docket.enable(true); } else { docket.enable(false); } docket.apiInfo(metaData()).select(). apis(RequestHandlerSelectors.basePackage("hk.com.hkbn.itrbss.controller")). paths(PathSelectors.any()).build(); return docket; } private ApiInfo metaData() { return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl("") .version(version).build(); } }
Add api spec info in Controller class:
@ApiOperation(value="queryBMOadvancenoticebyaddresscode.",response=List.class) @RequestMapping(value="/chk-bmo-notice",method=RequestMethod.GET) publicStringgetBmoAdvanceNotice( @RequestHeader("addr_ref_value")StringaddrRefValue, @RequestHeader("order_type")StringorderType){ MoRuleVictimDtomoRule=newMoRuleVictimDto(); moRule.setAddrRefValue(addrRefValue); returnmoRuleService.getBmoAdvanceNotice(moRule,orderType); }
使用:https://ip/project_name/swagger-ui.html