forked from mianshenglee/my-example
-
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
mason
committed
Nov 29, 2019
1 parent
f9fc934
commit 754edbc
Showing
14 changed files
with
656 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
logs/ | ||
target/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
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,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.1.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>me.mason.demo</groupId> | ||
<artifactId>logback-advance-demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>logback-advance-demo</name> | ||
<description>Demo advance project for Spring Boot and logback</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 工具包:hutool --> | ||
<dependency> | ||
<groupId>cn.hutool</groupId> | ||
<artifactId>hutool-core</artifactId> | ||
<version>5.0.6</version> | ||
</dependency> | ||
<!--工具包:lombok--> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
19 changes: 19 additions & 0 deletions
19
...ogback-advance-demo/src/main/java/me/mason/demo/advlogback/LogbackAdvDemoApplication.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,19 @@ | ||
package me.mason.demo.advlogback; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* 启动类 | ||
* | ||
* @author mason | ||
* @since 2019/10/31 | ||
*/ | ||
@SpringBootApplication | ||
public class LogbackAdvDemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(LogbackAdvDemoApplication.class, args); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...demo/logback-advance-demo/src/main/java/me/mason/demo/advlogback/config/WebAppConfig.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,28 @@ | ||
package me.mason.demo.advlogback.config; | ||
|
||
import me.mason.demo.advlogback.interceptor.RequestIdTraceInterceptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
/** | ||
* @Description web配置 | ||
* @Author Mason | ||
* @Since 2019/11/28 | ||
**/ | ||
@Configuration | ||
public class WebAppConfig implements WebMvcConfigurer { | ||
@Autowired | ||
RequestIdTraceInterceptor requestIdTraceInterceptor; | ||
|
||
/** | ||
* 添加拦截器 | ||
* @param registry | ||
*/ | ||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
//添加requestId | ||
registry.addInterceptor(requestIdTraceInterceptor); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...ogback-advance-demo/src/main/java/me/mason/demo/advlogback/controller/UserController.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,107 @@ | ||
package me.mason.demo.advlogback.controller; | ||
|
||
import me.mason.demo.advlogback.model.User; | ||
import me.mason.demo.advlogback.service.UserService; | ||
import me.mason.demo.advlogback.vo.ResponseResult; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
|
||
/** | ||
* User控制器 | ||
* | ||
* @author mason | ||
* @since 2019/10/31 | ||
*/ | ||
@RestController | ||
@RequestMapping("/users") | ||
public class UserController { | ||
@Autowired | ||
private UserService userService; | ||
|
||
/** | ||
* 查询单个用户 | ||
* @param userId | ||
* @return | ||
*/ | ||
@GetMapping("/{userId}") | ||
public ResponseResult<User> getUserById(@PathVariable long userId) { | ||
return ResponseResult.ok(userService.getUserById(userId)); | ||
} | ||
|
||
/** | ||
* 查询多个用户 | ||
* @return | ||
*/ | ||
@GetMapping() | ||
public ResponseResult<List<User>> getUsers() { | ||
return ResponseResult.ok(userService.getUsers()); | ||
} | ||
|
||
|
||
/** | ||
* 添加多个用户 | ||
* @param users | ||
* @return | ||
*/ | ||
@PostMapping() | ||
public ResponseResult<String> addUsers(@RequestBody List<User> users) { | ||
if (userService.addUsers(users) > 0) { | ||
return ResponseResult.ok("users added"); | ||
} else { | ||
return ResponseResult.err("ADD_USER_ERROR", "添加多个用户失败", "add exception"); | ||
} | ||
} | ||
|
||
/** | ||
* 添加单个用户 | ||
* @param user | ||
* @return | ||
*/ | ||
@PostMapping("/user") | ||
public ResponseResult<String> addUser(@RequestBody User user) { | ||
if (userService.addUser(user) > 0) { | ||
return ResponseResult.ok("user added"); | ||
} else { | ||
return ResponseResult.err("ADD_USER_ERROR", "添加用户失败", "add exception"); | ||
} | ||
} | ||
|
||
/** | ||
* 更新单个用户 | ||
* @param user | ||
* @return | ||
*/ | ||
@PutMapping("/user") | ||
public ResponseResult<String> updateUser(@RequestBody User user) { | ||
if (userService.updateUser(user) > 0) { | ||
return ResponseResult.ok("user updated"); | ||
} else { | ||
return ResponseResult.err("UPDATE_USER_ERROR", "更新用户失败", "update exception"); | ||
} | ||
} | ||
|
||
/** | ||
* 删除单个用户 | ||
* @param userId | ||
* @return | ||
*/ | ||
@DeleteMapping("/user") | ||
public ResponseResult<String> deleteUser(long userId) { | ||
if (userService.deleteUser(userId) > 0) { | ||
return ResponseResult.ok("user deleted"); | ||
} else { | ||
return ResponseResult.err("DELETE_USER_ERROR", "删除用户失败", "delete exception"); | ||
} | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...ce-demo/src/main/java/me/mason/demo/advlogback/interceptor/RequestIdTraceInterceptor.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,59 @@ | ||
package me.mason.demo.advlogback.interceptor; | ||
|
||
import cn.hutool.core.util.IdUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* @Description 拦截器,添加request-id | ||
* @Author Mason | ||
* @Since 2019/11/28 | ||
**/ | ||
@Slf4j | ||
@Component | ||
public class RequestIdTraceInterceptor implements HandlerInterceptor { | ||
|
||
public static final String REQUEST_ID_KEY = "request-id"; | ||
|
||
/** | ||
* 根据请求参数或请求头判断是否有“x-request-id”,有则使用,无则创建 | ||
* | ||
* @param request | ||
* @return 返回x-request-id的唯一标识 | ||
*/ | ||
public static String getRequestId(HttpServletRequest request) { | ||
String requestId; | ||
String parameterRequestId = request.getParameter(REQUEST_ID_KEY); | ||
String headerRequestId = request.getHeader(REQUEST_ID_KEY); | ||
|
||
if (parameterRequestId == null && headerRequestId == null) { | ||
log.debug("no x-request-id in request parameter or header"); | ||
requestId = IdUtil.simpleUUID(); | ||
} else { | ||
requestId = parameterRequestId != null ? parameterRequestId : headerRequestId; | ||
} | ||
|
||
return requestId; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
MDC.put(REQUEST_ID_KEY, getRequestId(request)); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { | ||
//把requestId添加到响应头,以便其它应用使用 | ||
response.addHeader(REQUEST_ID_KEY, MDC.get(REQUEST_ID_KEY)); | ||
//请求完成,从MDC中移除requestId | ||
MDC.remove(REQUEST_ID_KEY); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...-logback-demo/logback-advance-demo/src/main/java/me/mason/demo/advlogback/model/User.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,38 @@ | ||
package me.mason.demo.advlogback.model; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* 用户类 | ||
* | ||
* @author mason | ||
* @since 2019/10/31 | ||
*/ | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class User { | ||
/** | ||
* id | ||
*/ | ||
private long id; | ||
/** | ||
* 姓名 | ||
*/ | ||
private String name; | ||
/** | ||
* 年龄 | ||
*/ | ||
private int age; | ||
/** | ||
* 密码 | ||
*/ | ||
private String password; | ||
|
||
|
||
} |
Oops, something went wrong.