Step-1
Create a pojo to define format of the message
package com.bas.admin.exception.handler;
public class BASErrorMessage {
private String status;
private String code;
private String message;
private String ex;
private String moreInfo;
public BASErrorMessage(String status, String code, String message, String ex,
String moreInfo) {
this.status = status;
this.code = code;
this.message = message;
this.ex = ex;
this.moreInfo = moreInfo;
}
}
Create a pojo to define format of the message
package com.bas.admin.exception.handler;
public class BASErrorMessage {
private String status;
private String code;
private String message;
private String ex;
private String moreInfo;
public BASErrorMessage(String status, String code, String message, String ex,
String moreInfo) {
this.status = status;
this.code = code;
this.message = message;
this.ex = ex;
this.moreInfo = moreInfo;
}
}
Step-2
/**
*
* @author nagendra
* This class is for handling global exception
*
*/
@ControllerAdvice
public class BASGlobalExceptionHandler {
@ExceptionHandler(SQLException.class)
String handlerDbException(HttpServletRequest req, Exception ex,Model model) {
BASErrorMessage basErrorMessage=new BASErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.toString(),"5000" , ex.getMessage(), ex.getClass().toString(), req.getRequestURL().toString());
errorLoggerAsString(ex);
model.addAttribute("basErrorMessage", basErrorMessage);
return NavigationConstant.COMMON_PREFIX_PAGE+NavigationConstant.COMMON_ERROR_PAGE;
}
@ExceptionHandler(Exception.class)
String handlerGlobalException(HttpServletRequest req, Exception ex,Model model) {
BASErrorMessage basErrorMessage=new BASErrorMessage(HttpStatus.OK.toString(),"5000" , ex.getMessage(), ex.getClass().toString(), req.getRequestURL().toString());
errorLoggerAsString(ex);
model.addAttribute("basErrorMessage", basErrorMessage);
return NavigationConstant.COMMON_PREFIX_PAGE+NavigationConstant.COMMON_ERROR_PAGE;
}
Step-3
Scan the package for Global Exception Handler
/WEB-INF/bas-context.xml (Spring Web Application Context )
<!-- Scanning controller for annotation for spring mvc -->
<context:component-scan base-package="com.bas.hr.web.controller"/>
<context:component-scan base-package="com.bas.common.web.controller"/>
<context:component-scan base-package="com.bas.employee.web.controller"/>
<context:component-scan base-package="com.bas.admin.web.controller"/>
<context:component-scan base-package="com.bas.admin.web.report.controller"/>
<context:component-scan base-package="com.bas.admin.exception.handler"/>
/**
*
* @author nagendra
* This class is for handling global exception
*
*/
@ControllerAdvice
public class BASGlobalExceptionHandler {
@ExceptionHandler(SQLException.class)
String handlerDbException(HttpServletRequest req, Exception ex,Model model) {
BASErrorMessage basErrorMessage=new BASErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.toString(),"5000" , ex.getMessage(), ex.getClass().toString(), req.getRequestURL().toString());
errorLoggerAsString(ex);
model.addAttribute("basErrorMessage", basErrorMessage);
return NavigationConstant.COMMON_PREFIX_PAGE+NavigationConstant.COMMON_ERROR_PAGE;
}
@ExceptionHandler(Exception.class)
String handlerGlobalException(HttpServletRequest req, Exception ex,Model model) {
BASErrorMessage basErrorMessage=new BASErrorMessage(HttpStatus.OK.toString(),"5000" , ex.getMessage(), ex.getClass().toString(), req.getRequestURL().toString());
errorLoggerAsString(ex);
model.addAttribute("basErrorMessage", basErrorMessage);
return NavigationConstant.COMMON_PREFIX_PAGE+NavigationConstant.COMMON_ERROR_PAGE;
}
Step-3
Scan the package for Global Exception Handler
/WEB-INF/bas-context.xml (Spring Web Application Context )
<!-- Scanning controller for annotation for spring mvc -->
<context:component-scan base-package="com.bas.hr.web.controller"/>
<context:component-scan base-package="com.bas.common.web.controller"/>
<context:component-scan base-package="com.bas.employee.web.controller"/>
<context:component-scan base-package="com.bas.admin.web.controller"/>
<context:component-scan base-package="com.bas.admin.web.report.controller"/>
<context:component-scan base-package="com.bas.admin.exception.handler"/>
No comments:
Post a Comment