之前写了一篇Swagger生成API文档的文章,在后来应用过程中发现应用的版本不对,后来进行了调整,由于最近3个都非常的忙,一直到今天才更新:
maven依赖:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.10</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;
/**
* @author allen.shen
* @date 2016年12月29日
*
* description:
*/
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfiguration {
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.dxhr.platform.api";
public static final String VERSION = "1.0.0";
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API")
.description("接口描述文档")
.license("")
.licenseUrl("http://www.xxx.com")
.termsOfServiceUrl("")
.version(VERSION)
.description("描述")
.build();
}
@Bean
public Docket customImplementation(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any())
.build();
}
}
添加一个config.properties的文件
springfox.documentation.swagger.v2.path=/api-docs
在spring-mvc的配置文件中加入
<context:property-placeholder location="classpath:config.properties" />
@ApiOperation(httpMethod = "POST", value = "分配角色", notes = "分配角色")
@ApiResponses({
@ApiResponse(response = Long.class, code = StatusCodes.OK, message = "分配角色成功"),
@ApiResponse( code = StatusCodes.INTERNAL_SERVER_ERROR, message = "服务器运行错误"),
@ApiResponse( code = StatusCodes.PERMISSION_DENIED, message = "没有权限"),
@ApiResponse( code = StatusCodes.INVALID_PARAMETER, message = "参数错误"),
@ApiResponse( code = StatusCodes.NOT_FOUND, message = "未发现该员工"),
@ApiResponse( code = PermissionConstants.EMPLOYEE_NOT_IN_COMPANY, message = "公司没有该员工"))
然后就可以在controller的方面上应用啦。