1. 程式人生 > >在spring boot專案中應用swagger2

在spring boot專案中應用swagger2

1.       在pom.xml中新增
<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.6.1</version>

</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->

<dependency>

<groupId

>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.6.1</version>

</dependency>

2.       在SpringBoot Application同級新增一個Swagger config的類,隨意命名,示例程式碼如下:

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2

;

@Configuration

@EnableSwagger2

@ComponentScan("com.onCloud.yukechina.web")

publicclass SwaggerConfigAPI {

@Bean

public Docket createRestApi(){

returnnew Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());

   }

private ApiInfo apiInfo(){

returnnew ApiInfoBuilder().title("salesforce轉發伺服器").description("接收雲科資料,並將資料轉發到salsesforce").termsOfServiceUrl("www.yunkechina.com").version("salse").build();

   }

}

3.       最後在掃描的包中註釋要生成API文件的類,如下:

@RestController

publicclass SampleController {

@RequestMapping("/")

@ApiOperation(value="歡迎頁面",httpMethod="GET",notes="歡迎介面",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)

       String home() {

return"Wellcome";

       }

}

4.啟動spring boot 在瀏覽器中訪問http://localhost:80/swagger-ui.html