美文网首页
springboot:整合swagger

springboot:整合swagger

作者: 会飞的_雨 | 来源:发表于2020-04-11 17:13 被阅读0次

先用idea 创建一个springboot 工程,在pom.xml 中导入swagger2 的依赖包

   <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

添加配置类

package com.hy.blockchain.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zxy
 * @date 2020/4/11 16:42
 */
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        // 添加请求参数,我们这里把token作为请求头部参数传入后端
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();

        parameterBuilder.name("token")
                        .description("令牌")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false).build();
        parameters.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
                .build().globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Swagger API 文档")
                .description("This is a restful api document of Swagger.")
                .version("1.0")
                .build();
    }
}

添加控制器

  在项目中创建一个restful 风格的TestController
package com.hy.blockchain.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zxy
 * @date 2020/4/11 17:18
 */
@RequestMapping("/hello")
@RestController
//类注解
@Api(value = "describe of class")
public class HelloController {

    @RequestMapping("/index")
//    方法描述注解
    @ApiOperation(value = " describe of the method")
    // 参数说明注解
    public String hello(@ApiParam(value = "describe of the param",required = false)String name){
        return "hello swagger";
    }
}

相关文章

网友评论

      本文标题:springboot:整合swagger

      本文链接:https://www.haomeiwen.com/subject/oesumhtx.html