I would like to handle all portlet exceptions in one controller. This facility is proposed by spring using @ControllerAdvice.
I wonder if this feature still availabe and applicable in portlet context.
Note that I test it but the method handling the exception does not fire.
thanks in advance.
EDIT 1 Here is some code snippets
SpringMvcPortlet.java
@Controller
@RequestMapping("VIEW")
public class SpringMvcPortlet {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringMvcPortlet.class);
@Autowired
private MyService myService;
@RenderMapping
public String view(final RenderRequest request, final RenderResponse response) {
return "view";
}
@RenderMapping(params = "action=renderOne")
public String renderOne(final RenderRequest request, final RenderResponse response) {
boolean result = myService.doSomething();
if (!result){
throw new InitException("CAN NOT INITIALIZE APP")
}
return "renderOne";
}
//Doing an Ajax call here
@ResourceMapping("initParams")
public void getInitParams(ResourceRequest request, ResourceResponse response) throws InitException{
final JSONObject initParams = constructJsonObject();
if (initParams == null){
throw new InitException("CAN NOT INITIALIZE APP")
}
try {
response.getWriter().write(initParams.toString());
} catch (IOException e) {
LOGGER.error("ERROR :: "+e)
}
}
}
ExceptionControllerAdvice.java
@ControllerAdvice("com.xxx.yyy.portlet")
@RequestMapping(value="/")
public class ExceptionControllerAdvice {
@ExceptionHandler(InitException.class)
public ModelAndView handleInitException(InitException e) {
ModelAndView mav = new ModelAndView("exception");
mav.addObject("name", e.getClass().getSimpleName());
mav.addObject("message", e.getMessage());
return mav;
}
}
This way the method handling the exception in ExceptionControllerAdvice
is not fired when the service returns false or when doing an ajax call and initParams == null
.
In addition, when I put handleInitException(InitException e)
in portlet controller and throw an InitException in renderOne the exception is handled and rendering exception.jsp
view. However, in this case when an exception is thrown in ajax call (getInitParams
) the exception handler method is executed but not rendering the exception view.
So to resume, I do not know if ControllerAdvice is available in portlet context or if I am missing something. I do not understand also why in ajax processing the exception view is not rendered although the exception handler method was executed.