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

样式一:

 
 
spring cloud 集成 swagger2 构建Restful APIS 说明文档

样式二:

如果删除限制条件,会出现很多不想要的数据。
 spring cloud 集成 swagger2 构建Restful APIS 说明文档
 

spring cloud 集成 swagger2 构建Restful APIS 说明文档

 
 
上一篇:Android系统编程入门系列之加载服务Service


下一篇:LY.JAVA面向对象编程.工具类中使用静态、说明书的制作过程、API文档的使用过程