Descriptor Exceptions on Spring MVC Portlets

I would like to handle all portlet exceptions in one controller. This object is proposed by spring using @ControllerAdvice.

Interestingly, this feature is still available and applicable in the context of the portlet.

Please note that I am testing it, but the exception handling method does not work.

early.

EDIT 1 Here are 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;
}
}

Thus, the exception handling method in ExceptionControllerAdvicedoes not start when the service returns false or when making an ajax and call initParams == null.

, handleInitException(InitException e) InitException renderOne, exception.jsp. , ajax (getInitParams) , , .

, , , ControllerAdvice - . , ajax , .

+4
1

blog:

Spring Liferay REST Extjs. ( ExceptionHandler)

, blog Spring:

0

Source: https://habr.com/ru/post/1629401/


All Articles