-
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
0 parents
commit 314ae39
Showing
9 changed files
with
244 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,32 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/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/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/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,49 @@ | ||
<?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.5.2</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>me.fulln</groupId> | ||
<artifactId>log-helper</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>1.2.74</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
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,7 @@ | ||
# logHelper | ||
|
||
### Reference Documentation | ||
该项目是用来测试与观察`RequestBodyAdviceAdapter`中的整个代码流程,以便判断是否能够做全局日志处理,功能实现只依赖以下包: | ||
* [Spring Web](https://docs.spring.io/spring-boot/docs/2.5.2/reference/htmlsingle/#boot-features-developing-web-applications) | ||
|
||
如果你跟我一样觉得做一个日志打印,还有引入`AOP`这么多包,可以考虑使用`spring-mvc`中的相关功能去实现对应逻辑 |
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 com.log.helper; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
/** | ||
* @author: fulln | ||
* @description: log helper | ||
* @date : Created in 2021/7/4 . | ||
*/ | ||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/log/helper/advice/RestControllerAdviceHandle.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,57 @@ | ||
package com.log.helper.advice; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* @author fulln | ||
* @version 0.0.1 | ||
* @program logHelper | ||
* @description advice handle | ||
* @date 2021/7/4 15:25 | ||
**/ | ||
@Slf4j | ||
@RestControllerAdvice | ||
public class RestControllerAdviceHandle extends RequestBodyAdviceAdapter { | ||
|
||
/** | ||
* 日志打印 | ||
* @param body 入参 | ||
*/ | ||
public void setLog(Object body) { | ||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | ||
//仅仅打印请求参数 | ||
String s = JSON.toJSONString(body); | ||
// 打印日志 | ||
log.info("请求路径:" + request.getRequestURI() + ",参数" + s); | ||
} | ||
|
||
@Override | ||
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
setLog(null); | ||
return body; | ||
} | ||
|
||
@Override | ||
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
setLog(body); | ||
return body; | ||
} | ||
|
||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/log/helper/controller/AdviceController.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,40 @@ | ||
package com.log.helper.controller; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.log.helper.pojo.Car; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.commons.CommonsMultipartFile; | ||
|
||
@RestController | ||
@RequestMapping("/log") | ||
public class AdviceController { | ||
|
||
@RequestMapping("/advice/1") | ||
public String advice(@RequestParam String code) { | ||
return code; | ||
} | ||
|
||
@RequestMapping("/advice/2") | ||
public String adviceMutParams(@RequestParam String code, @RequestParam String params) { | ||
return code + params; | ||
} | ||
|
||
@RequestMapping("/advice/3") | ||
public String adviceJSON(@RequestBody Car code) { | ||
return JSON.toJSONString(code); | ||
} | ||
|
||
@RequestMapping("/advice/4") | ||
public String adviceJSONAndParams(@RequestBody Car code, @RequestParam String params) { | ||
return JSON.toJSONString(code) + params; | ||
} | ||
|
||
@RequestMapping("/advice/5") | ||
public String adviceFile(@RequestParam String code,@RequestParam CommonsMultipartFile file) { | ||
return code; | ||
} | ||
|
||
} |
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 com.log.helper.pojo; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @author fulln | ||
* @version 0.0.1 | ||
* @program logHelper | ||
* @description | ||
* @date 2021/7/4 15:19 | ||
**/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Car implements Serializable { | ||
|
||
private static final long serialVersionUID = 2199389397682166899L; | ||
|
||
private String make; | ||
private String build; | ||
private Integer numberOfSeat; | ||
private String type; | ||
|
||
} |
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 @@ | ||
|
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,13 @@ | ||
package com.log.helper; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class DemoApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |