spring boot整合swagger2实践

pom文件增加依赖

        <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.hp.careful.config;

import io.swagger.annotations.ApiOperation;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))   //这里采用包含注解的方式来确定要显示的接口
//                .apis(RequestHandlerSelectors.basePackage("com.hp.careful.controller"))    //这里采用包扫描的方式来确定要显示的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Hy Doc")
                .description("careful Api文档")
                .termsOfServiceUrl("NO terms of service")
                .contact("careful")
                .version("1.0")
                .build();
    }

}

启动类增加配置

package com.hp.careful;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class CarefulApplication {

    public static void main(String[] args) {
        SpringApplication.run(CarefulApplication.class, args);
    }

}

创建接口类并注解API

package com.hp.careful.controller;

import com.alibaba.fastjson.JSON;
import com.hp.careful.common.BaseResponse;
import com.hp.careful.dao.entity.VankeCustInfoEntity;
import com.hp.careful.service.VankeCustInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/cust")
@Api(tags = "客户接口")
public class CustController {

    @Autowired
    VankeCustInfoService vankeCustInfoService;

    @PostMapping("/save")
    @ApiOperation(value = "存入客户")
    @ApiImplicitParam(name = "Authorization", value = "用户Token", defaultValue = "Bearer ", required = true, dataType = "string", paramType = "header")
    public BaseResponse saveCustInfo(@RequestBody VankeCustInfoEntity vankeCustInfoEntity){
        log.info("saveCustInfo vankeCustInfoEntity: {}",JSON.toJSONString(vankeCustInfoEntity));
        vankeCustInfoService.insert(vankeCustInfoEntity);
        return BaseResponse.success();
    }

}

启动项目访问地址

http://localhost:port/swagger-ui.html#/

你可能感兴趣的:(spring boot整合swagger2实践)