在一些接口項目中,API的使用很頻繁,因此一款API在線文檔生成和測試工具很是有必要。而Swagger UI就是這麼一款很實用的在線工具
本博客介紹如何在公司或者本身的電腦上按照Swagger UI,本博客介紹一下怎麼集成到SpringBoot項目中,Swagger能夠安裝在線使用,安裝教程能夠參考我以前的博客,安裝在linux系統的,https://smilenicky.blog.csdn.net/article/details/70276327html
而後介紹一下怎麼集成到SpringBoot項目linux
maven加上配置web
<!-- swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
SpringBoot Application類:redis
package org.muses.jeeplatform; import org.muses.jeeplatform.cache.RedisClient; import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.bind.annotation.RequestMapping; /** * @author caiyuyu */ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, MybatisAutoConfiguration.class}) @ServletComponentScan @EnableScheduling @EnableTransactionManagement //@EnableCaching @EnableAsync @Controller public class Application { @Autowired RedisClient redisClient; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RequestMapping("/set") public String set(String key, String value) throws Exception{ redisClient.setValue(key, value); return "success"; } @RequestMapping("/get") public String get(String key) throws Exception { return redisClient.getValue(key); } // @Bean // public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { // PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer(); // c.setIgnoreUnresolvablePlaceholders(true); // return c; // } }
新建一個配置類,記得加上主鍵@EnableSwagger2spring
package org.muses.jeeplatform.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; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * <pre> * Swagger2配置類 * </pre> * * @author nicky * <pre> * 修改記錄 * 修改後版本: V1.0.1 修改人: 修改日期: 2019年05月12日 修改內容: * </pre> */ @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("org.muses.jeeplatform.web")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("Swagger2") .description("SpringBoot集成Swagger2構建RESTful API接口") .termsOfServiceUrl("https://smilenicky.blog.csdn.net") .contact("Nicky.Ma") .version("1.0.1") .build(); } }
能夠對整個Controller進行註釋
對接口註釋,包括具體的傳參
實體類:api
package org.muses.jeeplatform.core; import io.swagger.annotations.ApiModelProperty; import lombok.Data; /** * <pre> * 接口返回類 * </pre> * * @author nicky * <pre> * 修改記錄 * 修改後版本: 修改人: 修改日期: 2019年05月12日 修改內容: * </pre> */ @Data public class ResultVO<T> { @ApiModelProperty("狀態碼") private Integer status; @ApiModelProperty("返回信息") private String message; @ApiModelProperty("返回數據") private T data; public ResultVO(Integer status, String message){ this.status = status; this.message = message; } public ResultVO(Integer status, String message, T data){ this.status = status; this.message = message; this.data = data; } public static <T> ResultVO error(String message) { return new ResultVO(0, message, null); } public static <T> ResultVO error(String message,T data) { return new ResultVO(0, message, data); } public static <T> ResultVO successful(String message){ return new ResultVO(1,message,null); } public static <T> ResultVO successful(String message, T data){ return new ResultVO(1, message, data); } }
因此工程就部署好了,訪問springboot
http://localhost:8080/${項目名稱}/swagger-ui.html
能夠看到接口的詳情信息,Swagger2至關於一個在線文檔
mybatis