下面代码的注释部分、若把APIBase类的访问限制改为private、或者protected、则在login requestmapping方法上throw出的ParameterErrorException不能被APIBase里的handleParameterErrorException方法捕获(将会被其父类Base的handleException方法捕获)。
[color=red][b]故在使用JAVA的注解时一定要注意所注解的类是否为public的。[/b][/color]
[color=red][b]故在使用JAVA的注解时一定要注意所注解的类是否为public的。[/b][/color]
public class UserController {
public static class Base {
@ExceptionHandler(Exception.class)
public @ResponseBody
Object handleException(HttpServletResponse response, Exception e) throws Exception {
System.out.print("handle exception");
return "ok";
}
}
public static class ParameterErrorException extends Exception{}
//APIBase类可见性改为private或protected都会有问题
//private static class APIBase extends Base {
//protected static class APIBase extends Base {
public static class APIBase extends Base {
@ExceptionHandler(value={ParameterErrorException.class})
public @ResponseBody
Object handleParameterErrorException(ParameterErrorException e) throws Exception {
System.out.print("handle ParameterErrorException");
return "ok";
}
}
public static class Ajaxs{
@Controller
public static class Login extends APIBase {
@RequestMapping(value = Urls.USER.AJAX_LOGIN, method = RequestMethod.POST)
@ResponseBody
public Object login(@ModelAttribute LoginVO loginVO, BindingResult br)
throws Exception {
if(br.hasErrors()){
throw new Exception();
}
if(loginVO.getLoginName() == null){
throw new ParameterErrorException();
}
return "OK";
}
}
@Controller
public static class Logout extends APIBase {
@RequestMapping(value = Urls.USER.AJAX_LOGOUT, method = RequestMethod.GET)
@ResponseBody
public Object logout()
throws Exception {
return "OK";
}
}
}
}