springboot整合knife4j

本文来说下springboot整合knife4j的学习与使用

文章目录

  • 程序实例
  • knife4j使用


程序实例

maven导入


<dependency>
    <groupId>com.github.xiaoymingroupId>
    <artifactId>knife4j-spring-boot-starterartifactId>
    <version>3.0.3version>
dependency>

config配置

package com.wideth.config;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfig {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("文档标题")
                .description("文档描述")
                .termsOfServiceUrl("服务条款地址")
                .version("文档版本")
                .license("开源版本号")
                .licenseUrl("开源地址")
                .contact(new Contact("作者名", "作者网址", "作者邮箱"))
                .build();
    }

}

knife4j使用

访问网址

http://localhost:9988/doc.html

访问页面

springboot整合knife4j_第1张图片

controller测试

package com.wideth.controller;


import com.wideth.entity.Order;
import com.wideth.response.ResponseBean;
import com.wideth.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/order")
@Api(tags = "订单的基本信息")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;


    @GetMapping("/getOrderInfo")
    @ApiOperation(value = "订单的基本信息")
    public ResponseBean<?> getOrderInfo() {

        List<Order> data = iOrderService.getOrderInfo();
        if (data == null || data.size() == 0) {
            return new ResponseBean<>(400, "fail", Collections.emptyList());
        }
        return new ResponseBean<>(200, "success", data);
    }


}

接口使用

springboot整合knife4j_第2张图片

你可能感兴趣的:(spring,mvc,springboot,后端,java)