国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring Boot 使用Swagger2自動生成RESTful API文檔

2019-11-08 20:14:34
字體:
來源:轉載
供稿:網友
API文檔自動生成工具有很多種,比如:SwaggerSPRing REST DocsRAMLApiDocJSSpringRestDoc這篇文章對這幾種比較流行的API文檔生成工具進行了評估和對比:https://opencredo.com/rest-api-tooling-review/

Swagger是屬于比較推薦的一種。

Swagger的特點:1、在Spring Boot中配置非常簡單2、項目部署時,根據代碼自動生成文檔,通過html展示3、代碼改動后,項目重新部署時文檔會自動更新,無需手動更新文檔4、保證了代碼和文檔的一致性,不會出現不一致的情況5、可以直接在文檔界面上測試接口,無需再利用第三方來調試接口了幾個簡單的步驟,就可以在Spring Boot中配置Swagger2來實現API文檔自動生成:1、引入依賴:
		<!-- 文檔自動生成 -->		<dependency>			<groupId>io.springfox</groupId>			<artifactId>springfox-swagger2</artifactId>			<version>2.6.1</version>		</dependency>		<dependency>			<groupId>io.springfox</groupId>			<artifactId>springfox-swagger-ui</artifactId>			<version>2.6.1</version>		</dependency>2、創建Swagger2配置類:
@Configuration@EnableSwagger2public class Swagger2 {	@Bean	public Docket createRestApi(){		return  new Docket(DocumentationType.SWAGGER_2)				.apiInfo(apiInfo())				.select()				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))				.paths(PathSelectors.any())				.build();	}	private ApiInfo apiInfo(){		return new ApiInfoBuilder()				.title("專題頁api文檔")				.description("專題頁api文檔")				.termsOfServiceUrl("http://terms-of-services.url")				.version("1.0")				.build();	}}通過@Configuration注解,讓Spring來加載該類配置;再通過@EnableSwagger2注解來啟用Swagger2。apis()可以指定有某個注解的方法需要生成API文檔,還可以指定掃描哪個包來生成文檔,比如:
apis(RequestHandlerSelectors.basePackage("com.xjj.web.controller"))3、在Controller的方法中注解各種文檔描述:
@RestController@RequestMapping("/topic/")public class TopicController {	protected final Logger logger = LoggerFactory.getLogger(this.getClass());	@Autowired	TopicService topicService;	@RequestMapping(value="getTopic", method = RequestMethod.GET)	@ApiOperation(value="接口描述。。。", response = TopicResult.class)	@ApiImplicitParams({			@ApiImplicitParam(name = "sn", value = "編號", required = true, dataType = "String", paramType = "query"),			@ApiImplicitParam(name = "token", value = "用戶token", required = true, dataType = "String", paramType = "query")	})	public JsonResult getTopicBySn(HttpServletRequest request, @RequestParam String sn, @RequestParam String token){		JsonResult result = null;		try {			Topic topic = topicService.getTopic(sn);			if(topic == null){				result = new JsonResult(ReturnCode.PARAMS_ERROR, "錯誤");			}else {				result = new JsonResult(ReturnCode.SUCCESS, "成功", topic);			}		}catch (Exception e){			logger.error("getTopicBySn Exception", e);			result = new JsonResult(ReturnCode.EXCEPTION);		}		return result;	}}其中,response = TopicResult.class 表示返回值使用JsonResult的子類TopicResult來展示更詳細的內容;如果不指定,則使用返回值JsonResult來生成文檔。這兩個類也需要相應的注解(@ApiModel和@ApiModelProperty):
@ApiModelpublic class JsonResult {	@ApiModelProperty(value = "狀態碼", example="40001", required = true, position=-2)	private String code;	@ApiModelProperty(value = "返回消息", example="恭喜,成功!", required = true, position=-1)	private String message;	@ApiModelProperty(value = "具體數據")	private Object data;	//constructors, getters, setters 略...}
@ApiModelpublic class TopicResult extends JsonResult {	@ApiModelProperty(value = "專題詳情", required = true)	private Topic data;	//constructors, getters, setters 略...}對于里面的Topic類,也需要相應的@ApiModel和@ApiModelProperty注解(代碼略)。對于Controller中的參數注解,也可以直接放到參數列表中,比如:
@RestController@RequestMapping("/apply/")public class ApplyController {	protected final Logger logger = LoggerFactory.getLogger(this.getClass());	@Autowired	ApplyService applyService;	@RequestMapping(value="store-mgr-setup", method = RequestMethod.POST)	@ApiOperation(value="接口描述")	public JsonResult applyStoreMgrSetup(HttpServletRequest request,					@ApiParam(value = "參數描述", required = true) @RequestBody DianApply dianApply){		JsonResult result = null;		//其他代碼略...		return result;	}}4、API文檔查看和調試項目啟動后,訪問這個url就可以看到自動生成的API文檔了:http://localhost:8080/<project-name>/swagger-ui.html

測試:填入相應的參數后,點擊下方“Try it out!”按鈕,即可完成了一次請求調用!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长阳| 大英县| 阿尔山市| 昭苏县| 虞城县| 额济纳旗| 喜德县| 正宁县| 永安市| 类乌齐县| 长兴县| 株洲市| 黔西| 格尔木市| 体育| 涞源县| 安溪县| 会昌县| 太仓市| 达拉特旗| 仙居县| 哈巴河县| 建昌县| 达日县| 洞头县| 黄龙县| 新田县| 乌兰县| 香河县| 嘉荫县| 钦州市| 太和县| 东宁县| 津南区| 开鲁县| 兰坪| 农安县| 宁陵县| 彰化县| 潍坊市| 固阳县|