-
Notifications
You must be signed in to change notification settings - Fork 21
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
sayHello
committed
Mar 25, 2024
1 parent
5841c81
commit 95ea412
Showing
4 changed files
with
53 additions
and
55 deletions.
There are no files selected for viewing
68 changes: 34 additions & 34 deletions
68
src/main/java/com/coco/boot/aspect/ResponseHeaderAspect.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 |
---|---|---|
@@ -1,34 +1,34 @@ | ||
package com.coco.boot.aspect; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Aspect | ||
@Component | ||
public class ResponseHeaderAspect { | ||
|
||
@Pointcut("execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController)") | ||
public void controllerMethods() { | ||
} | ||
|
||
@Around("controllerMethods()") | ||
public Object apiResponseAdvice(ProceedingJoinPoint pjp) throws Throwable { | ||
Object proceed = pjp.proceed(); | ||
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
|
||
if (sra != null) { | ||
HttpServletResponse response = sra.getResponse(); | ||
if (response != null) { | ||
response.setHeader("Content-Type", "application/json; charset=utf-8"); | ||
} | ||
} | ||
|
||
return proceed; | ||
} | ||
} | ||
//package com.coco.boot.aspect; | ||
// | ||
//import jakarta.servlet.http.HttpServletResponse; | ||
//import org.aspectj.lang.ProceedingJoinPoint; | ||
//import org.aspectj.lang.annotation.Around; | ||
//import org.aspectj.lang.annotation.Aspect; | ||
//import org.aspectj.lang.annotation.Pointcut; | ||
//import org.springframework.stereotype.Component; | ||
//import org.springframework.web.context.request.RequestContextHolder; | ||
//import org.springframework.web.context.request.ServletRequestAttributes; | ||
// | ||
//@Aspect | ||
//@Component | ||
//public class ResponseHeaderAspect { | ||
// | ||
// @Pointcut("execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController)") | ||
// public void controllerMethods() { | ||
// } | ||
// | ||
// @Around("controllerMethods()") | ||
// public Object apiResponseAdvice(ProceedingJoinPoint pjp) throws Throwable { | ||
// Object proceed = pjp.proceed(); | ||
// ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
// | ||
// if (sra != null) { | ||
// HttpServletResponse response = sra.getResponse(); | ||
// if (response != null) { | ||
// response.setHeader("Content-Type", "application/json; charset=utf-8"); | ||
// } | ||
// } | ||
// | ||
// return proceed; | ||
// } | ||
//} |
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
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