湛江网站建设维护,云服务器官网首页,深圳网页设计兴田德润i简介,注册一个网站多少钱?我已经一年没有从头开始开发Spring Web应用程序了#xff0c;如果我不参加QA自动化工程师的培训#xff0c;那么这段时间甚至会更长。 由于这个原因#xff0c;我开发了一个示例REST应用程序。 除了Swagger#xff0c;一切对我来说都很熟悉。 因此#xff0c;我将描述我在… 我已经一年没有从头开始开发Spring Web应用程序了如果我不参加QA自动化工程师的培训那么这段时间甚至会更长。 由于这个原因我开发了一个示例REST应用程序。 除了Swagger一切对我来说都很熟悉。 因此我将描述我在Spring Boot和Swagger UI中获得的新经验。 前言 首先我需要提到我已经使用过Spring IO。 是的这是我第一次将Spring用作流行的Java框架而不是平台。 真是令人兴奋。 与我以前的Spring经验相比Spring IO的配置过程和项目设置变得更加轻松快捷。 根据培训主题示例Web应用程序需要具有简单的业务逻辑。 我决定开发一个应用程序使房东房地产经纪人可以管理他们的房地产公寓。 因此应用程序的用户可以对房东和公寓执行CRUD操作。 现在当您知道在什么情况下必须使用swagger时我可以省略关于应用程序和培训的其余故事而跳到本文的主要主题-Swagger和Spring Boot集成。 Spring靴招摇 为了将Swagger插入Web Spring应用程序您需要向构建文件Maven或Gradle添加依赖项。 它在Git官方页面上非常简单明了。 之后您可以添加一个单独的配置Java类该类负责Swagger import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.*;Configuration
EnableSwagger
EnableAutoConfiguration
public class SwaggerConfig {private SpringSwaggerConfig springSwaggerConfig;Autowiredpublic void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {this.springSwaggerConfig springSwaggerConfig;}Beanpublic SwaggerSpringMvcPlugin customImplementation() {return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)//This info will be used in Swagger. See realisation of ApiInfo for more details..apiInfo(new ApiInfo(SmartMe education API,This app is for education, training purpose. It represents model of landlords and apartments for rent,null,null,null,null))//Here we disable auto generating of responses for REST-endpoints.useDefaultResponseMessages(false)//Here we specify URI patterns which will be included in Swagger docs. Use regex for this purpose..includePatterns(/landlords.*);}} 配置文件完成后您可以继续使用Controllers。 顺便说一句您需要通过Spring Boot Application类将swagger配置放入扫描区域。 Api(basePath /landlords, value Landlords, description Operations with Landlords, produces application/json)
RestController
RequestMapping(value /landlords, produces MediaType.APPLICATION_JSON_VALUE)
public class LandLordController {private static final Logger logger LoggerFactory.getLogger(LandLordController.class);Autowiredprivate LandLordService landLordService;RequestMapping(method RequestMethod.POST,consumes MediaType.APPLICATION_JSON_VALUE)ResponseStatus(HttpStatus.CREATED)ApiOperation(value Create new Landlord, notes Creates new Landlord)ApiResponses(value {ApiResponse(code 400, message Fields are with validation errors),ApiResponse(code 201, message ) })public LandLord createLandLord(Valid RequestBody LandLordDTO landLordDTO) {logger.info(LandLord DTO is: landLordDTO);LandLord landLord new LandLord(landLordDTO);landLordService.create(landLord);return landLord;}//Other endpoints are omitted
} 这就是所有需要JSON格式的API文档。 要检查它请启动您的应用程序并转到http// localhost8080 / api-docs Spring Boot Swagger用户界面 很好的JSON格式的API文档很好但是对其他团队成员例如前端开发人员没有太大帮助。 因此我们必须插入UI。 从官方git repo下载swagger ui。 之后将其解压缩并复制dist目录并将其粘贴到src / java / resources中的文件夹 / public或/ static或/ resources中。 现在在swagger中重命名dist 。 打开index.html并更改JavaScript代码它应如下所示 $(function () {var url window.location.search.match(/url([^])/);if (url url.length 1) {url decodeURIComponent(url[1]);} else {url /api-docs;}
//rest of code... 这就对了。 现在重新启动应用程序并导航到http// localhost8080 / swagger / index.html 您必须看到以下内容 翻译自: https://www.javacodegeeks.com/2015/03/spring-boot-swagger-ui.html