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


修改異常處理方法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;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM