本文共 1657 字,大约阅读时间需要 5 分钟。
1.如何使用SimpleMappingExceptionResolver?
步骤一:创建MySimpleMappingExceptionResolver.java
package com.springboot.demo.SpringBootDemoProject.error;import java.util.Properties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;//@Configurationpublic class MySimpleMappingExceptionResolver { @Bean public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver(); Properties properties = new Properties(); properties.put("java.lang.NullPointerException", "error/error_null"); simpleMappingExceptionResolver.setDefaultErrorView("error/error_other"); simpleMappingExceptionResolver.setExceptionMappings(properties); return simpleMappingExceptionResolver; }}
步骤二:创建ErrorTestController.java
package com.springboot.demo.SpringBootDemoProject.error;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("e")public class ErrorTestController { @RequestMapping("show") public String show(ModelMap model) { String str = null; str.charAt(0); return "thymeleaf"; }}
2.MySimpleMappingExceptionResolver和MyExceptionHandler在启动时间有什么不同之处?
不同之处: MySimpleMappingExceptionResolver启动是在tomcat启动的时候执行 并不是在controller执行的时候执行
而MyExceptionHandler启动时间在每次url访问的时候启动
转载地址:http://qrzqf.baihongyu.com/