Handle errors using a custom view - spring

I want to handle custom errors like 401, 403, 500 etc in Spring 3.2 application using Filters . I do not want to use the web.xml approach of defining the error pages.
I tried to follow the solution listed at How to handle exceptions thrown while rendering a view in Spring MVC?
But ErrorHandler.handle does not really exist.
How can I use filters to catch all the errors and redirect to different views ?

A good way to handle exceptions in Spring Controllers is using the #ExceptionHanldler #ControllerAdvice annotations. Here is the relevant part of the documentation and here is a tutorial on their usage.

Related

What's the bare minimum topics to cover from JSP and Servlet to learn SpringBoot?

So I Want to build projects using spring Boot directly as I'm under a bit of time crunch. Please help me out.
What you need to know about JSP: nothing. JSP is old and clunky, use Thymeleaf instead. But if you're building single page web apps you don't need any templates, you just need to know how to accept and return JSON.
Servlets: you need to understand the threading model, that there's only one servlet being traversed by multiple threads, and any instance variables will be accessed by all the http request threads. More than servlets you need to know filters, because Spring Security uses them.

Best place to implement exception handling in spring MVC applications

I am new to spring framework and wanted to know which would be the best place to implement exception handling logic - controller or service? Or if it should be done at all the layers in my code?
It is best practice to handle exceptions wherever it is encountered. If i focus on question in thread, it should be handled on both cases i.e. controller and services along with other places where there is possibility of exception.
For controller point of view i would suggest use Global Exception Handling With ControllerAdvice, even if one is using controlleradvice should also handle exception in service, utils, handlers and other classes.
Refrences: https://dzone.com/articles/global-exception-handling-with-controlleradvice

How can I populate dynamic information on a custom Tomcat 404 page?

I'm putting together a web app using Tomcat + Spring, and I'd like to have custom 404 pages which contain dynamic content, not just a static html file. For example, if I were working on a blog site, I might want to show a list of the 5 most recent posts on the 404 page to direct the user to actual content.
Additionally, is there a way I can leverage the error information when generating the error page? For example, if the error involved an exception happening in my app, as opposed to a bad url, can I display the stack trace?
Are you creating the project from scratch? Are you able to use Spring-Boot? Spring Boot provides a lot of nice error handling functionality out of the box that can be easily modified.
Either way, if you just want to use standard error page mapping in the web.xml, or example:
<error-page>
<error-code>404</error-code>
<location>/error/404</location>
</error-page>
When your application returns a 404 status code, the request will be intercepted and forwarded internally to the endpoint /error/404 - which can just be any endpoint in your spring app, so can do any dynamic stuff that you would in a normal controller.
To do it programatically, without Spring Boot, you could create a Filter class that wraps all requests and just catches exceptions and checks for non 2XX response codes and forwards/handles appropriately - this is basically the solution that the Spring Boot guys implemented (see the ErrorPageFilter class).
To access info about the exception, that can be accessed from the request - see here for similar answer: https://stackoverflow.com/a/1034667/258813

Hibernate, spring and thymeleaf : exception handling

I'm working on a spring mvc web application lately using thymeleaf as view engine, hibernate as persistence unit and spring security. I opted for an XML-less approch (using javaConfig)
I want to provide some customized error pages and not only one global page for all the errors.
I'm new to hibernate and such, so my question is , what are the exceptions that i should handel, for example when the application can't reach the database or something like that, what kind of exceptions does hibernate throw, and for thymeleaf too, how can catch the exceptions it throws ?
One more thing, can i handle the 404 errors : for example, if the user enter an inexisting url, i want to redirect him to an error page, or to the home page, how should i proceed using java config ?
Without more specifics, the standard and generic response for providing custom error handling and error pages in Spring MVC is documented in the following section within the Spring Reference: 17.11 Handling exceptions. This section is sufficient to answer all the error page/handling questions you've mentioned.
For Hibernate, the it should throw an org.hibernate.HibernateException or subtype of it. Handling org.hibernate.HibernateException should be sufficient for UI purposes. For a list of exceptions, the org.hibernate.HibernateException API docs for your Hibernate version will have a list of all known subtypes. For example: http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/HibernateException.html

A heavily customized Spring Web application and the dispatcher servlet

We have a web application that uses spring, struts and camel right now and there is a lot of customization we have done to allow us to know when beans are added to the context.
So, we have gotten to a point where we would like to remove struts from the application, because we are only using it to handle actions and we figure we could either use spring or camel to do the same thing. So I was able to get it to work with camel/velocity, but we didn't like how we really couldn't use the request object directly in the jsp (afaik, you have to put everything in the header of the Exchange and in the jsp you would do ${header.someReqVariableName}).
So we wanted to go the spring route, but since we load the context.xml directly, we have a provider that extends ContextSingletonBeanFactoryLocator and we pass the xml file name as a param, we haven't been able to figure out how to get the DispatcherServlet to work without giving it another configuration xml.
Is there a way to either:
Have camel use jsp for processing a jsp (and have all the usage of jsp tags)?
or
Have spring to see that a context has already been loaded and use that instead of having another new one?
or
Something better I have thought up?
You can use camel-jetty to expose HTTP endpoints, but I wouldn't use it for any complex web app development (JPS, etc). I'd use use Spring MVC (or similar) and use Camel for any complex routing/messaging requirements...
Here is another way, you can use the producer template to send the request to the camel context if you can get the reference of the camel context from the spring.

Resources