Swagger生成Rest API

之前写了一篇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的方面上应用啦。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值