您的位置:首页 > 编程语言 > Java开发

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

2017-01-18 10:59 489 查看
在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>

[/code]
Application.java
packagecom.yyit.marketOperation.message;


importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;


importspringfox.documentation.swagger2.annotations.EnableSwagger2;


@SpringBootApplication

@EnableSwagger2
//启动swagger注解
publicclassApplication{


	publicstaticvoidmain(String[]args){

		//TODOAuto-generatedmethodstub

		SpringApplication.run(Application.class,args);

}


}

[/code]

引入了一个注解@EnableSwagger2来启动swagger注解。(启动该注解使得用在controller中的swagger注解生效,覆盖的范围由@ComponentScan的配置来指定,这里默认指定为根路径"com.xxx.firstboot"下的所有controller)

在与Application.java同级目录中,创建一个新文件swagger2.java
swagger2.java
packagecom.liuwq.marketOperation.message;


importjava.time.LocalDate;


importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.ComponentScan;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.http.ResponseEntity;

importorg.springframework.web.client.RestTemplate;


importspringfox.documentation.builders.ApiInfoBuilder;

importspringfox.documentation.builders.PathSelectors;

importspringfox.documentation.builders.RequestHandlerSelectors;

importspringfox.documentation.service.ApiInfo;

importspringfox.documentation.spi.DocumentationType;

importspringfox.documentation.spring.web.plugins.Docket;

importspringfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration

@ComponentScan(basePackages={"com.liuwq.marketOperation.*"})

@EnableSwagger2

publicclassSwagger2{


@Bean

	publicDocketpetApi(){

		returnnewDocket(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);

}


	privateApiInfoapiInfo(){

		returnnewApiInfoBuilder()

				.title("Springcloud中使用Swagger2构建RestfulAPIs")

.description("微信墙,内容相关接口")

.termsOfServiceUrl("git@github.com:LiuwqGit/spring-boot-eureka.git")

.contact("liuwq").version("1.0").build();

}


@Bean

	publicRestTemplaterestTemplate(){


		returnnewRestTemplate();

}

}

[/code]


样式一:


样式二:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐