使用RESTful API可以更好的開發前后分離的應用,后面一節會介紹使用模版引擎Beetl開發后端渲染的應用。
一、配置Swagger(Api 接口文檔)
1、使用系統自帶
拷貝jeesite-modules-swagger中的config.SysApiConfig和swagger.config.SwaggerComfig到com.jeesite.modules下面
將SysApiConfig修改為CmsApiConfig
// // 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根據需要修改
2、自己配置
pom.xml
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.java
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啟動容器加注解@EnableSwagger2
此種方式可以查看下面兩篇文章
使用swagger作為restful api的doc文檔生成
SpringBoot(七):SpringBoot整合Swagger2
二、寫接口程序
此時返回前端的是實體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; }
三、http://localhost:8980/swagger-ui.html