1. 程式人生 > >SpringBoot學習14:springboot異常處理方式4(使用SimpleMappingExceptionResolver處理異常)

SpringBoot學習14:springboot異常處理方式4(使用SimpleMappingExceptionResolver處理異常)

mapping oot 方法 lan lob admin reat framework fig

修改異常處理方法3中的全局異常處理Controller即可

package bjsxt.exception;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.Properties; /** * Created by Administrator on 2019/2/14. * 全局異常處理類,使用SimpleMappingExceptionResolver 做全局異常處理 * 優點:直接在一個方法裏對需要處理的異常跳轉不同的視圖,比較簡單方便 * 缺點:無法把錯誤信息傳遞到視圖層
*/ @Configuration public class GlobalException { /** * * @return */ @Bean public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){ SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver(); Properties properties=new Properties();
/** * 參數一:異常的類型,註意必須是異常類型的全名 * 參數二:視圖名稱 */ properties.put("java.lang.ArithmeticException","error_arithmetic"); properties.put("java.lang.NullPointerException","error_nullPointer"); resolver.setExceptionMappings(properties); return resolver; } }

SpringBoot學習14:springboot異常處理方式4(使用SimpleMappingExceptionResolver處理異常)