Spring MVC – 15 – Exception Handling – 01 | SimpleMappingExceptionResolver
Merhaba Arkadaslar
Bu bolumde Exception Handling konusunu inceleyecegiz. Gercek uygulamalari dusundugumuzde mutlaka beklenmedik hatalara (exception/error) karsi son kullaniciya(end user) uygun bir hata aciklamasi gostermemiz gerekir.
Spring MVC de Exception Handling kavramini XML veya Annotation ile uygulayabiliriz.
Bunun icin kullanacagimiz sinif ve annotationlar ;
- SimpleMappingExceptionResolver
- @ExceptionHandler
- @ResponseStatus
- @ControllerAdvice
Spring MVC & SimpleMappingExceptionResolver
Bu bolumde XML konfigurasyonu ile Exception Handling uygulamasini gelistirelim. SimpleMappingExceptionResolver icin bean tanimini gerceklestirelim ;
14.exception.handling.xmlBased.xml
exceptionMapping icin duzenlemeleri yapalim ;
private Properties exceptionMappings;
java.lang.NumberFormatException firlatildiginda numberFormat.jsp sayfasi
java.lang.NullPointerException firlatildiginda npe.jsp sayfasi
_14.exception.handling.xmlBased.model.DatabaseException firlatildiginda dbException.jsp sayfasi exception handling icin kullanilacak.
... <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.NumberFormatException">14.exception.handling.xmlBased.view/numberFormat</prop> <prop key="java.lang.NullPointerException">14.exception.handling.xmlBased.view/npe</prop> <prop key="_14.exception.handling.xmlBased.model.DatabaseException"> 14.exception.handling.xmlBased.view/dbException </prop> </props> </property> </bean> ...
SimpleMappingExceptionResolverController.java
Test amacli Controller sinifimizda ilgili exceptionlari firlatacak (throw) sekilde metotlar yazalim.
Dikkat edecek olursak test amacli XML dosyamizda (14.exception.handling.xmlBased.xml) tanimladigimiz NumberFormatException , NullPointerException ve DatabaseException firlatan 3 tane metot tanimladik.
Not : Tabi @Controller ve @RequestMapping icin gerekli XML konfigurasyonu , package tanimlarini unutmayalim.
package _14.exception.handling.xmlBased.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import _14.exception.handling.xmlBased.model.DatabaseException; @Controller public class SimpleMappingExceptionResolverController { @RequestMapping("/numberFormatException") //http://localhost:8080/injavawetrust.springmvc/numberFormatException public ModelAndView throwNumberFormatException() { throw new NumberFormatException(); } @RequestMapping("/nullPointerException") //http://localhost:8080/injavawetrust.springmvc/nullPointerException public ModelAndView throwNullPointerException() { throw new NullPointerException(); } @RequestMapping("/databaseException") //http://localhost:8080/injavawetrust.springmvc/databaseException String throwDatabaseException() throws Exception { throw new DatabaseException("DB-1000", "DB Connection timeout..."); } }
DatabaseException.java
package _14.exception.handling.xmlBased.model; public class DatabaseException extends RuntimeException { private static final long serialVersionUID = 1L; private String errCode; private String errMsg; //getters and setters public DatabaseException(String errCode, String errMsg) { this.errCode = errCode; this.errMsg = errMsg; } }
JSP sayfalarimiz test amacli olacak ve basit olarak ;
NOT : exception JSP implicit objedir.
Servlet & JSP – 18 – Implicit Object & JSP Lifecycle
numberFormat.jsp
<html> <body> <h2>numberFormat.jsp</h2> <h2>${exception}</h2> </body> </html>
npe.jsp
<html> <body> <h2>npe.jsp</h2> <h2>${exception}</h2> </body> </html>
dbException.jsp
<html> <body> <h2>dbException.jsp</h2> <h2>${exception.errCode}</h2> <h2>${exception.errMsg}</h2> </body> </html>
web.xml
Konfigurasyon dosyasi olarak /WEB-INF/14.exception.handling.xmlBased.xml dosyamizi aktif edelim;
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> <!-- /WEB-INF/01.appContext.xml /WEB-INF/02.00.appContext.xml /WEB-INF/03.multiActionController.xml .... --> /WEB-INF/14.exception.handling.xmlBased.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Uygulamamizi deploy edelim , test etmek icin ;
http://localhost:8080/injavawetrust.springmvc/numberFormatException
http://localhost:8080/injavawetrust.springmvc/nullPointerException
http://localhost:8080/injavawetrust.springmvc/databaseException
Test sonuclarini inceledigimizde , Controller sinifimizda tanimladigimiz ilgili metotlar tarafindan istekler karsilanacaktir. Bu metotlarda throw anahtar kelimesiyle exception firlatiyoruz , (tabi gercek uygulamalarda bu durum beklenmedik hata oldugunda kullaniciya anlamli bir hata mesaji cikartmak icin kullanilacaktir ) , firlatilan hatalara gore npe.jsp , numberFormat.jsp ve dbException.jsp sayfasi gosterilmektedir.
Github kaynak kodlar / source folder
Injavawetrust-springmvc-tutorial
Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer
Leave a Reply