在springboot中集成swagger

springboot集成swagger

1.依赖

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.5.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>
    dependencies>

2.配置类

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;

/**
 * @author Administrator
 */

@Configuration
@EnableSwagger2
public class Swagger2Config {


    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xiya"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("费师傅的api文档")
                .description("feishifudeapiwendang")
                .version("v0.1")
                .build();
    }
}
  1. 使用注解
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */

@RestController
@Api(value = "提供页面的增删改查",description = "提供页面的增删改查")
public class HelloController {

    @ApiOperation("提供页面的增加")
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,
                    paramType = "path",dataType = "int",defaultValue = "1",example = "1"),
            @ApiImplicitParam(name = "size",value = "每页的条数",required = true,
                    paramType = "path",dataType = "int",defaultValue = "10",example = "10")
    })
    public String hello(){
        return "hello world!!!";
    }
}

你可能感兴趣的:(java)