gpt4 book ai didi

java - Spring框架MVC中AOP的实现

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:58 24 4
gpt4 key购买 nike

我正在尝试实现 RESTful API 与 Spring MVC 。在我的应用程序中使用 AOP Aspect 时,我的编码搞砸了。我需要在每个来自源的请求中检查 authToken ,以便我可以授予授权,并且我还需要它来进行日志记录。

在我的代码中,我有几个自定义异常类继承自另一个主自定义异常类,该类是从Exception扩展的。当引发自定义异常时,我需要等待异常的预期方法在我的 Aspect 类中起作用,以便我可以返回适当的错误 JSON 响应。

但是,在我的代码中,两种等待异常的方法都有效,我想知道我是否以正确的方式编写了异常类。另一件事,我不知道如何判断我的方面类不适用于我定义的某些操作。

这是我的代码:

@Aspect
public class SowLoggerAOP {

protected Logger logger = Logger.getLogger("SowLoggerAOP");
@Autowired(required = true)
private HttpServletRequest request;
@Resource(name = "personService")
private PersonService personService;

@Pointcut("execution(* sow.webservice.controllers..*.*(..))")
private void selectAll(){}

@Before("execution( * sow.webservice.controllers.PersonController.*(..))")
public void logBeforeReq(JoinPoint jp) throws Exception{
String personId = request.getParameter(Consts.AUTH_TOKEN);
if (personService.isUserValid(personId)) {
logger.debug("Spring AOP! Before invocation SUCCESFULL!!!: ");
}
else {
logger.error("Person not found for the " + Consts.AUTH_TOKEN + " : " + personId);
throw new PersonNotFoundException(
"Person not found for the " + Consts.AUTH_TOKEN + " : " + personId,
ErrorCodes.PERSON_NOT_FOUND);
}
}

@AfterThrowing(pointcut = "selectAll()", throwing = "sowEx")
public SowResult throwingSowException(SowCustomException sowEx){
int errorCode = ErrorCodes.GENERAL_SYSTEM_ERROR;
if(sowEx.getErrorCode() != 0)
errorCode = sowEx.getErrorCode();
SowResult result = new SowResult(Consts.SOW_RESULT_ERROR,
errorCode,
sowEx.getMessage(),
"islem hata almistir!!");

System.out.println("There has been an exception: " + sowEx.toString());
return result;
}

@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void throwingJavaException(Exception ex){
System.out.println("JAVA EXCEPTION IS THROWN :");
}
}

public class SowCustomException extends Exception{

private int errorCode;

public SowCustomException(){
super();
}

public SowCustomException(String errMsg){
super(errMsg);
}
public SowCustomException(String errMsg, int errorCode){
super(errMsg);
this.errorCode = errorCode;
}

public SowCustomException(int errorCode){
super();
this.errorCode = errorCode;
}

public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}
public class PersonNotFoundException extends SowCustomException{

public PersonNotFoundException(){
super();
}

public PersonNotFoundException(String errMsg){
super(errMsg);
}

public PersonNotFoundException(String errMsg, int errorCode){
super(errMsg);
super.setErrorCode(errorCode);
}

public PersonNotFoundException(int errorCode){
super();
super.setErrorCode(errorCode);
}
}

必须从该类创建预期的 JSON:

public class SowResult {

public int resultCode; // 1- Succesfull, 0- Fail
public int errorCode;
public String errorString;
public String message;
public Date date;
public SowResult(int resultCode, int errorCode, String errorString,
String message) {
this.resultCode = resultCode;
this.errorCode = errorCode; // 0 - No Error
this.errorString = errorString;
this.message = message;
this.date = new Date();
}
}

最佳答案

正如Sotirios Delimanolis所说,如果你想在拦截抛出异常的方法后返回一些东西,你应该使用@Around。像这样的东西(我相信你的切入点是正确的):

@Around("selectAll()")
public Object handleException(ProceedingJoinPoint pjp) throws Throwable {
int errorCode = ErrorCodes.GENERAL_SYSTEM_ERROR;
if(sowEx.getErrorCode() != 0)
errorCode = sowEx.getErrorCode();
SowResult retVal = null;

try {
retVal = (SowResult) pjp.proceed();
} catch (RequestValidationException sowEx) {
//now return the error response
return new SowResult(Consts.SOW_RESULT_ERROR,
errorCode,
sowEx.getMessage(),
"islem hata almistir!!");
}

return retVal;
}

关于java - Spring框架MVC中AOP的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com