forked from javastacks/spring-boot-best-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1729f4c
commit 724c5e8
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
17 changes: 17 additions & 0 deletions
17
spring-boot-knife4j/src/main/java/cn/javastack/springboot/knife4j/Knife4jApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
spring-boot-knife4j/src/main/java/cn/javastack/springboot/knife4j/api/Knife4jController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ot-knife4j/src/main/java/cn/javastack/springboot/knife4j/config/Knife4jConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
knife4j: | ||
# 开启增强 | ||
enable: true | ||
# 开启登录认证 | ||
basic: | ||
enable: true | ||
username: test | ||
password: test |