Tuesday, July 31, 2018

SpringRest GET calls showing the HTML Page for Invalid API -- throwExceptionIfNoHandlerFound

In Spring Framework, there are number of ways of handing exceptions (and particularly 404 error). Here is a documentation link.
  • First, you can still use error-page tag in web.xml, and customize error page. Here is an example.
  • Second, you can use one @ExceptionHandler for all controllers, like this:
    @ControllerAdvice
    public class ControllerAdvisor {
    
         @ExceptionHandler(NoHandlerFoundException.class)
         public String handle(Exception ex) {
    
            return "404";//this is view name
        }
    }
    For this to work, set throwExceptionIfNoHandlerFound property to true for DispatcherServletin web.xml:
    <init-param>
        <param-name>throwExceptionIfNoHandlerFound</param-name>
        <param-value>true</param-value>
    </init-param>
    You can also pass some objects to error view, see javadoc for this.

No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...