Skip to content

Commit

Permalink
Spring Boot + Knife4j
Browse files Browse the repository at this point in the history
  • Loading branch information
javastacks committed Oct 26, 2021
1 parent 1729f4c commit 724c5e8
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>spring-boot-flyway</module>
<module>spring-boot-mapstruct</module>
<module>spring-boot-jasypt</module>
<module>spring-boot-knife4j</module>
</modules>
<packaging>pom</packaging>

Expand Down
28 changes: 28 additions & 0 deletions spring-boot-knife4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-best-practice</artifactId>
<groupId>cn.javastack</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-knife4j</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<knife4j-spring-boot-starter.version>2.0.9</knife4j-spring-boot-starter.version>
</properties>

<dependencies>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j-spring-boot-starter.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.javastack.springboot.knife4j;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* 来源微信公众号:Java技术栈
* 作者:栈长
*/
@SpringBootApplication
public class Knife4jApplication {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cn.javastack.springboot.knife4j.api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
* Knife4j 测试接口
* 来源微信公众号:Java技术栈
* 作者:栈长
*/
@Api(tags = "测试模块")
@RestController
public class Knife4jController {

/**
* Knife4j 测试接口问好
* 来源微信公众号:Java技术栈
* 作者:栈长
*/
@ApiImplicitParam(name = "name", value = "名称", required = true)
@ApiOperation(value = "公众号Java技术栈向你问好!")
@ApiOperationSupport(order = 2, author = "栈长")
@GetMapping("/knife4j/hi")
public ResponseEntity<String> hello(@RequestParam(value = "name") String name) {
return ResponseEntity.ok("Hi:" + name);
}

/**
* Knife4j 测试接口登录
* 来源微信公众号:Java技术栈
* 作者:栈长
*/
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", required = true),
@ApiImplicitParam(name = "password", value = "密码", required = true)
})
@ApiOperation(value = "接口登录!")
@ApiOperationSupport(order = 1, author = "栈长")
@PostMapping("/knife4j/login")
public ResponseEntity<String> login(@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password) {
if (StringUtils.isNotBlank(username) && "javastack".equals(password)) {
return ResponseEntity.ok("登录成功:" + username);
}
return ResponseEntity.ok("用户名或者密码有误:" + username);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cn.javastack.springboot.knife4j.config;

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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
* Knife4j 配置类
* 来源微信公众号:Java技术栈
* 作者:栈长
*/
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

@Bean(value = "defaultDocket")
public Docket defaultDocket() {
// 联系人信息
Contact contact = new Contact("公众号:Java技术栈", "https://www.javastack.cn", "[email protected]");

// 创建 Docket
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("Knife4j 测试")
.description("Knife4j Test")
.termsOfServiceUrl("https://www.javastack.cn")
.contact(contact)
.version("1.0")
.build())
.groupName("1.x")
.select()
.apis(RequestHandlerSelectors.basePackage("cn.javastack.springboot.knife4j.api"))
.paths(PathSelectors.any())
.build();
return docket;
}

}
8 changes: 8 additions & 0 deletions spring-boot-knife4j/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
knife4j:
# 开启增强
enable: true
# 开启登录认证
basic:
enable: true
username: test
password: test

0 comments on commit 724c5e8

Please sign in to comment.