Java利用Swagger2自動生成對外接口的文檔
一直以來做對外的接口文檔都比較原始,基本上都是手寫的文檔傳來傳去,最近發(fā)現(xiàn)了一個新玩具,可以在接口上省去不少麻煩。
swagger是一款方便展示的API文檔框架。它可以將接口的類型最全面的展示給對方開發(fā)人員,避免了手寫文檔的片面和誤差行為。
swagger目前有兩種swagger和swagger2兩種,1比較麻煩,所以不考慮使用。本文主要記錄我用swagger2做對外接口的兩種方式,方面后面查閱。
一、使用傳統(tǒng)的springmvc整合swagger2
1、maven依賴
<!--springfox依賴--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.3</version> </dependency> <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、spring-mvc.xml 中添加映射靜態(tài)的配置(其實我項目中把這個去掉也可以,不知道什么情況):
<!-- swagger靜態(tài)文件路徑 --> <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/> <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
注意:基本的springmvc配置我就不貼了,需要注意的是,如果你看到swagger-ui.html 界面出來,但卻一片空白,請檢查下你web.xml中攔截器的配置,一定要springmvc先攔截到,然后界面才會顯示的。
3、然后是swagger2的配置類:
@Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("net.laoyeyey.yyblog")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("yyblog項目 RESTful APIs") .description("yyblog項目api接口文檔") .version("1.0") .build(); } }
注意:paths如果在生產(chǎn)情況下可以調(diào)整為PathSelectors.none(),即不顯示所有接口信息;
4、接口信息配置
即在SpringMvc的Controller中配置相關(guān)的接口信息
@Controller @RequestMapping(value = "aitou") @Api(description = "測試服務(wù)-賬戶信息查詢") public class DailyOperationDataController { Logger logger = Logger.getLogger(DailyOperationDataController.class); @Autowired private DailyOperationDataService DailyOperationDataService; /* * @ApiOperation(value = "接口說明", httpMethod ="接口請求方式", response ="接口返回參數(shù)類型", notes ="接口發(fā)布說明" * @ApiParam(required = "是否必須參數(shù)", name ="參數(shù)名稱", value ="參數(shù)具體描述" */ @ApiOperation(value = "賬戶信息查詢接口") @RequestMapping(method ={RequestMethod.POST,RequestMethod.GET}, value="/query/dailydata/{dataDate}") @ResponseBody public DailyOperationDataDto getDailyReportByDataDate(@PathVariable("dataDate") String dataDate) { try { return DailyOperationDataService.getDailyReportByDataDate(dataDate); } catch (Exception e) { logger.error(e.getMessage(), e); } return null; } }
注:通常情況下swagger2會將掃描包下所有的接口展示出來,這里我是對外的接口是單獨(dú)一個包,避免展示過多的接口,當(dāng)然接口方法也可以讓它不展示。可以在下面看到相關(guān)的注解中有寫。
常用的一些注解
@Api:用在類上,說明該類的作用
@ApiOperation:用在方法上,說明方法的作用
@ApiImplicitParams:用在方法上包含一組參數(shù)說明
@ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一個請求參數(shù)的各個方面
paramType:參數(shù)放在哪個地方
· header --> 請求參數(shù)的獲?。篅RequestHeader
· query -->請求參數(shù)的獲?。篅RequestParam
· path(用于restful接口)--> 請求參數(shù)的獲?。篅PathVariable
· body(不常用)
· form(不常用)
name:參數(shù)名
dataType:參數(shù)類型
required:參數(shù)是否必須傳
value:參數(shù)的意思
defaultValue:參數(shù)的默認(rèn)值
@ApiResponses:用于表示一組響應(yīng)
@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個錯誤的響應(yīng)信息
code:數(shù)字,例如400
message:信息,例如"請求參數(shù)沒填好"
response:拋出異常的類
@ApiParam:單個參數(shù)描述
@ApiModel:描述一個Model的信息,用對象來接收參數(shù)(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進(jìn)行描述的時候)
@ApiModelProperty:描述一個model的屬性
@ApiProperty:用對象接收參數(shù)時,描述對象的一個字段
@ApiIgnore:使用該注解忽略這個API
基本上就是上面這些了,是不是很easy,下面看下效果圖
二、使用springboot整合swagger2
上面說了使用傳統(tǒng)的springmvc整合swagger2,在說說最近比較流行的springboot的方式,其實原理都是一樣的。
1、maven依賴
<!--springfox依賴 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
這個是我最近用springboot寫的個人項目中的內(nèi)用,版本用的2.7.0
2、添加靜態(tài)資源配置
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter {/** * 配置靜態(tài)資源路徑以及上傳文件的路徑 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/upload/**").addResourceLocations(environment.getProperty("spring.resources.static-locations")); /*swagger-ui*/ registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
其實就是最后兩句,如果你不配置這個的話,訪問swagger-ui.html會出現(xiàn)500,還是404的錯誤來著,記不清了,應(yīng)該是404.
3、swagger2的配置類
和上面一樣,基本上沒區(qū)別
@Configuration @EnableSwagger2 @EnableWebMvc public class SwaggerConfig extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("net.laoyeye.yyblog.web.frontend")) .paths(PathSelectors.none()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("yyblog項目 RESTful APIs") .description("yyblog項目api接口文檔") .version("1.0") .build(); } }
注意,我上面有說path的問題哦,直接拷貝不顯示api的,(#^.^#)
4、接口的配置
/** * 前臺文章Controller * @author 小賣鋪的老爺爺 * @date 2018年5月5日 * @website www.laoyeye.net */ @Api(description="文章查詢") @Controller @RequestMapping("/article") public class ArticleController { @Autowired private ArticleService articleService; @Autowired private SettingService settingService; @Autowired private CateService cateService; @Autowired private TagReferService tagReferService; @Autowired private UserService userService; @Autowired private ArticleMapper articleMapper; @Autowired private CommentService commentService; @ApiOperation(value="文章查詢接口") @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "Long") @GetMapping("/{id}") public String index(Model model, @PathVariable("id") Long id) { try { articleService.updateViewsById(id); } catch (Exception ignore) { } List<Setting> settings = settingService.listAll(); Map<String,Object> map = new HashMap<String,Object>(); for (Setting setting : settings) { map.put(setting.getCode(), setting.getValue()); } Article article = articleService.getArticleById(id); model.addAttribute("settings", map); model.addAttribute("cateList", cateService.listAllCate()); model.addAttribute("article", article); model.addAttribute("tags", tagReferService.listNameByArticleId(article.getId())); model.addAttribute("author", userService.getNicknameById(article.getAuthorId())); //回頭改 model.addAttribute("articles", articleMapper.listArticleByTitle(null)); model.addAttribute("similars", articleMapper.listArticleByTitle(null)); CommentQuery query = new CommentQuery(); query.setLimit(10); query.setPage(1); query.setArticleId(id); model.addAttribute("comments", commentService.listCommentByArticleId(query)); return "frontend/article"; } @ApiOperation(value="文章評論查詢接口") @PostMapping("/comments") @ResponseBody public DataGridResult comments(CommentQuery query) { //設(shè)置默認(rèn)10 query.setLimit(10); return commentService.listCommentByArticleId(query); } @ApiOperation(value="文章點(diǎn)贊接口") @ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataType = "Long") @PostMapping("/approve") @ResponseBody public YYBlogResult approve(@RequestParam Long articleId) { return articleService.updateApproveCntById(articleId); } }
最后同樣來個效果圖,和上面一樣。
PathSelectors.none()的時候
PathSelectors.any()的時候
看到效果圖是不是接口內(nèi)容一目了然,很簡潔明了了。
最后,好像忘記說swagger文檔的路徑了
如果你的項目在根目錄:http://localhost:8080/swagger-ui.html
如果不是根目錄那就是:http://localhost:8080/你的項目名/swagger-ui.html
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程池ThreadPoolExecutor源碼深入分析
ThreadPoolExecutor作為java.util.concurrent包對外提供基礎(chǔ)實現(xiàn),以內(nèi)部線程池的形式對外提供管理任務(wù)執(zhí)行,線程調(diào)度,線程池管理等等服務(wù)2022-08-08阿里規(guī)范:為何boolean類型變量命名禁用is開頭
這篇文章主要給大家介紹了關(guān)于阿里規(guī)范:為何boolean類型變量命名禁用is開頭的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Spring Boot 數(shù)據(jù)校驗@Valid+統(tǒng)一異常處理的實現(xiàn)
這篇文章主要介紹了Spring Boot 數(shù)據(jù)校驗@Valid+統(tǒng)一異常處理的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04java使用listIterator逆序arraylist示例分享
對于列表而言,除了Iterator,還提供了一個功能更加強(qiáng)大的ListIterator。它可以實現(xiàn)逆序遍歷列表中的元素。本示例將使用其逆序遍歷ArrayList2014-02-02SpringBoot實現(xiàn)jsonp跨域通信的方法示例
這篇文章主要介紹了SpringBoot實現(xiàn)jsonp跨域通信的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring Boot日志收集及鏈路追蹤實現(xiàn)示例
這篇文章主要為大家介紹了Spring Boot日志收集及鏈路追蹤實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12springboot多環(huán)境進(jìn)行動態(tài)配置的方法
這篇文章主要介紹了springboot多環(huán)境下如何進(jìn)行動態(tài)配置,本文主要分享了如何在springboot的項目中使用多環(huán)境配置,重點(diǎn)是”spring.profiles.active“屬性,需要的朋友可以參考下2022-06-06