继续潜水

导航

 

了解到swagger 就记录下用法

rest.swaggerEnabled 配置文件设置为true开启swagger,false或者不配置,不开启swagger

 

pom.xml

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
Swagger2.java
package com.zjjw.manager.web.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
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;

/**
* Swagger的配置信息
*
* @author tzq
* @date 2019-03-18 16:11
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Value("${rest.swaggerEnabled:false}")
private Boolean restConfig ;

@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(restConfig)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zjjw.manager.web.controller"))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Cloud-Console API")
.description("检务后台管理API")
.version("1.0.0")
.build();
}
}
Controller注解
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
@RestController  
@RequestMapping("test") 
@Api(value="测试接口Controller")
public class TestController {
 
	@ApiOperation(value="测试用接口", notes="测试用接口" ,httpMethod="POST")
	@ApiImplicitParams({
		@ApiImplicitParam(name="name", value="用户姓名", dataType = "String", required=true, paramType="form"),
		@ApiImplicitParam(name="id", value="id", dataType = "int", required=false, paramType="form")
	})
    @RequestMapping("word")  
    public String HelloWord(String name,Integer id) {  
        return "Hello Word";  
    }
	
	

  

访问路径:ip+端口/swagger-ui.html

swagger  个人感觉,适合快速开发,减少些接口文档的时间,

 

posted on 2018-11-16 15:51  继续潜水  阅读(603)  评论(0编辑  收藏  举报