一、简述
开发前后端拆散架构的我的项目,往往调试后端Web接口须要用到POSTMAN工具。尽管POSTMAN工具的性能十分弱小,然而申请参数很多的状况下,咱们手写这些参数和数据还是十分麻烦的。因而咱们须要一个调试后端Web接口更加简便的办法。恰好Swagger提供了RESTAPI调用形式,咱们不须要借助任何工具的状况下,拜访Swagger页面,就能够对Web接口进行调用和调试,这种调试形式的效率要远超POSTMAN软件。
二、pom.xml中导入Swagger的依赖
<dependency>
<groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
</dependency>
三、创立Swagger的配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//java我的项目 www fhadmin org@Beanpublic Docket createRestApi() { Docket docket = new Docket(DocumentationType.SWAGGER_2); // ApiInfoBuilder 用于在Swagger界面上增加各种信息 ApiInfoBuilder builder = new ApiInfoBuilder(); builder.title("XXXX零碎"); ApiInfo apiInfo = builder.build(); docket.apiInfo(apiInfo); // ApiSelectorBuilder 用来设置哪些类中的办法会生成到游戏REST API中 ApiSelectorBuilder selectorBuilder = docket.select(); selectorBuilder.paths(PathSelectors.any()); //所有包下的类 //应用@ApiOperation的办法会被提取到REST API中 selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)); docket = selectorBuilder.build(); /* * 上面的语句是开启对JWT的反对,当用户用Swagger调用受JWT认证爱护的办法, * 必须要先提交参数(例如令牌) */ //存储用户必须提交的参数 List<ApiKey> apikey = new ArrayList(); //规定用户须要输出什么参数 apikey.add(new ApiKey("token", "token", "header")); docket.securitySchemes(apikey); //如果用户JWT认证通过,则在Swagger中全局无效 AuthorizationScope scope = newwww.sangpi.com AuthorizationScope("global", "accessEverything"); AuthorizationScope[] scopeArray = {scope}; //存储令牌和作用域 SecurityReference reference = new SecurityReference("token", scopeArray); List refList = new ArrayList(); refList.add(reference); SecurityContext context = SecurityContext.builder().securityReferences(refList).build(); List cxtList = new ArrayList(); cxtList.add(context); docket.securityContexts(cxtList); return docket;
四、测试Web接口
package com.gaoyang.emos.wx.controller;
import com.gaoyang.emos.wx.common.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//java游戏我的项目 www fhadmin org
@RestController
@RequestMapping("/test")
@Api("测试Web接口")
public class TestController {
@RequestMapping("testSwagger")@ApiOperation("测试Swagger配置")public ResponseResult testSwagger(){ return ResponseResult.error(200,"OK");}
}
watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=
五、拜访swagger-ui.html页面
留神:还须要增加在启动类中增加 @EnableSwagger2 注解,,否则会出线,页面加载失败状况!
启动我的项目之后,拜访: localhost:8080/emos-wx-api/swagger-ui.html