Knife4j 搭建使用

官网

首页:knife4j
文档:knife4j
gitee地址:knife4j: Knife4j是一个集Swagger2 和 OpenAPI3为一体的增强解决方案

官网文档:1.6 快速开始 | knife4j

依赖及配置

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.1.RELEASE</version>
</parent>
<dependencies>
    <!-- SpringBoot整合Web组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--整合Knife4j-->
    <dependency>
       <groupId>com.github.xiaoymin</groupId>
       <artifactId>knife4j-spring-boot-starter</artifactId>
       <version>3.0.3</version>
    </dependency>
</dependencies>
  • 在Swagger2Config中增加一个@EnableKnife4j注解,该注解可以开启knife4j的增强功能;
package com.everyday.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;

/**
* Swagger2 相关配置
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfig {
    
    /**
    * 创建api
    * @return
    */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .useDefaultResponseMessages(false)
            .apiInfo(apiInfo())
            .select()
            // api接口扫包范围
            .apis(RequestHandlerSelectors.basePackage("com.everyday.api"))
            .paths(PathSelectors.any())
            .build();
        
    }
    
    /**
    * 配置api信息
    *
    * @return
    */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .description("sys-admin-main系统Api接口文档")
            .contact(new Contact("codeWang", "https://www.baidu.com", "123@qq.com"))
            .version("v1.1.0")
            .title("API测试文档")
            .build();
    }
    
}

 代码

package com.everyday.api;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;


@Api(tags = "登录接口Service")
public interface LoginService {


    @ApiOperation(value = "登录接口", notes = "根据账号、密码 实现登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "账号", required = true),
            @ApiImplicitParam(paramType = "query", name = "psw", value = "密码", required = true)
    })
    @PostMapping("/login")
    String login(String code, String psw);
}
package com.everyday.api.ipml;

import com.everyday.api.LoginService;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginServiceIpml implements LoginService {

    @Override
    public String login(String code, String psw) {
        if ("codeWang".equals(code) && "1234".equals(psw)) {
            return "登录成功";
        }
        return "用户名密码错误,请重新输入!!!";
    }
}

测试

knife4j跟Tomcat共用一个端口,我设置的Tomcat是8099。访问地址:

http://127.0.0.1:8099/doc.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值