spring cloud 集成 swagger2 構建Restful APIS 說明文檔

在Pom.xml文件中引用依賴
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <!--Swagger-->
  <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
  </dependency>
  <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
  </dependency>
  <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
  </dependency>
  <!-- actuator 健康檢查,檢測該服務是否正常運行 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
pom.xml

 

Application.java
 
package com.yyit.marketOperation.message;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2  //啓動swagger註解
public class Application{
public static void main(String[] args){
  // TODO Auto-generated method stub
  SpringApplication.run(Application.class, args);
  }
}
Application.java

 

  • 引入了一個註解@EnableSwagger2來啓動swagger註解。(啓動該註解使得用在controller中的swagger註解生效,覆蓋的範圍由@ComponentScan的配置來指定,這裏默認指定爲根路徑"com.xxx.firstboot"下的全部controller)
 
在與Application.java同級目錄中,建立一個新文件swagger2.java
 
swagger2.java 
 
package com.liuwq.marketOperation.message;
import java.time.LocalDate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
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;
@Configuration
@ComponentScan(basePackages ={"com.liuwq.marketOperation.*"})
@EnableSwagger2
public class Swagger2{
  @Bean
  public Docket petApi(){
    return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.liuwq.marketOperation.message"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .directModelSubstitute(LocalDate.class,String.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .enableUrlTemplating(true);
  }
  private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
                .title("Spring cloud 中使用Swagger2構建Restful APIs")
                .description("微信牆,內容相關接口")
                .termsOfServiceUrl("git@github.com:LiuwqGit/spring-boot-eureka.git")
                .contact("liuwq").version("1.0").build();
  }
  @Bean
  public RestTemplate restTemplate(){
    return new RestTemplate();
  }
}
swagger2.java

 

 

樣式一:

 
 

樣式二:

若是刪除限制條件,會出現不少不想要的數據。
 
 
相關文章
相關標籤/搜索