1 pom配置javascript
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
2 配置啓動類html
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; 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; /* * Restful API 訪問路徑: * http://IP:port/{context-path}/swagger-ui.html * eg:http://localhost:8080/swagger-ui.html */ @EnableSwagger2 @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.jingqbbridge.business.api.web") public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() // 選擇那些路徑和api會生成document .apis(RequestHandlerSelectors.any()) // 對全部api進行監控 .paths(PathSelectors.any()) // 對全部路徑進行監控 .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("鯨錢包開發者文檔") .termsOfServiceUrl("https://api.jingqb.com/rest") .contact("鯨錢包團隊") .version("1.0.0") .build(); } }
3 root-context.xml文件配置java
<!-- 使用 Swagger --> <bean class="com.jingqbbridge.frame.util.SwaggerConfig" />
4 servlet-context.xml文件配置git
<context:component-scan base-package="com.jingqbbridge" /> <!-- 使用 Swagger Restful API文檔時,添加此註解 --> <mvc:default-servlet-handler />
5 web.xml文件配置github
<servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
6 sampleweb
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.jingqbbridge.business.GlobalConstants; import com.jingqbbridge.business.annotation.AccessTypeAnnotation; import com.jingqbbridge.business.model.ExtMerchant; import com.jingqbbridge.business.util.MessageUtil; import com.jingqbbridge.business.util.SecurityUtil; import com.jingqbbridge.frame.base.web.BaseController; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import springfox.documentation.annotations.ApiIgnore; @RestController public class LoginApiController extends BaseController { public static final String getPlatTokenParamNote = "{<br>\"id\":\"商戶號\",<br>\"key\":\"商戶密鑰\"<br>}"; /** * 獲取平臺token * * @author Aaron.Wu * @date 2016-06-08 09:54:02 */ @RequestMapping(value = "/api/getPlatToken", method = RequestMethod.POST) @AccessTypeAnnotation(value = AccessTypeAnnotation.EXTERNAL) @ApiOperation(value = "獲取接口調用憑據", notes = "獲取接口調用憑據", tags={ "鯨錢包開發者文檔", }) public Map<String, Object> getPlatToken(@ApiParam(required = true, name = "paramMap", value=getPlatTokenParamNote) @RequestBody Map<String, String> paramMap) throws Exception { Map<String, Object> resultMap = new HashMap<String, Object>(); String id = paramMap.get("id"); String key = paramMap.get("key"); Map<String, ExtMerchant> merchantList = SecurityUtil.getExtMerchantList(); ExtMerchant extMerchant = merchantList.get(id); String platToken = ""; if (extMerchant != null && key.equals(extMerchant.getKey())) { platToken = SecurityUtil.getAccessToken(id); } else { return MessageUtil.addMsgToResultForExt("E10000", null); } resultMap.put("platToken", platToken); return MessageUtil.addMsgToResultForExt(GlobalConstants.Result.CODE_OK, resultMap); }
7 國際化-中文顯示配置「swagger-ui.html」中添加如下配置spring
<!-- Some basic translations --> <script src='webjars/springfox-swagger-ui/lang/translator.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lang/zh-cn.js' type='text/javascript'></script>
※參考資料↓api
swagger註解資料
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodelmvc
封裝swagger
http://www.sosoapi.com/app