我正在試圖實現一個RESTful API和Spring MVC。而我的編碼在使用AOP Aspect
的時候搞砸了。我需要在源代碼的每個請求中檢查一個authToken
,以便我可以授權並且還需要它進行日誌記錄。在Spring Framework MVC中實現AOP
在我的代碼中,我有幾個custom exception classes
從另一個主要customized exception class
繼承,這是從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();
}
}
你還沒有告訴我們什麼是或不是與此代碼工作 –
對不起!我在這裏添加了一些額外的信息 – Emilla