使用RESTful API能夠更好的開發先後分離的應用,後面一節會介紹使用模版引擎Beetl開發後端渲染的應用。html
1、配置Swagger(Api 接口文檔)前端
一、使用系統自帶java
拷貝jeesite-modules-swagger中的config.SysApiConfig和swagger.config.SwaggerComfig到com.jeesite.modules下面 web
將SysApiConfig修改成CmsApiConfigspring
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.jeesite.modules.config; import com.jeesite.modules.swagger.config.SwaggerConfig; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spring.web.plugins.Docket; @Configuration @ConditionalOnProperty( name = {"web.swagger.enabled"}, havingValue = "true", matchIfMissing = false ) public class CmsApiConfig { public CmsApiConfig() { } @Bean public Docket cmsApi() { String moduleCode = "cms"; String moduleName = "內容模塊"; String basePackage = "com.jeesite.modules.cms.web"; return SwaggerConfig.docket(moduleCode, moduleName, basePackage).select().apis(RequestHandlerSelectors.basePackage(basePackage)).build(); } }
SwaggerConfig根據須要修改json
二、本身配置後端
pom.xmlapi
swagger
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
SwaggerConfig.javarestful
package com.jeesite.modules.config; 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; /** * * @author Vito * */ @Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("yuosc-api") .select() .apis(RequestHandlerSelectors.basePackage("com.jeesite.modules.cms.web")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("YUOSC API") .description("YUOSC<a href='http://vote.yangtzeu.edu.cn' target='_blank'>門戶</a>" + "和<a href='http://localhost:8980/index' target='_blank'>博客</a>的API文檔") // .termsOfServiceUrl("http://localhost:8980/index") .version("1.0") .build(); } }
在Application啓動容器加註解@EnableSwagger2app
此種方式能夠查看下面兩篇文章
使用swagger做爲restful api的doc文檔生成
SpringBoot(七):SpringBoot整合Swagger2
2、寫接口程序
此時返回前端的是實體json
/** * 首頁內容(獲取top5文章) * @param * @return */ @ApiOperation(value="首頁內容(獲取top5文章)", notes="獲取最新top5文章") @GetMapping(value = "/index") public JsCmsResponse index(JsCmsResponse resp) throws Exception { List<JsCmsArticlesEntity> articles = this.frontService.getAllArticles(); resp.setRespCode(JsCmsResponse.RESPCODE_SUCCESS); resp.setMsgInfo("獲取內容成功"); resp.setRespObj(articles); return resp; }
3、http://localhost:8980/swagger-ui.html