1、过滤器:创建CharacterEncodingFilte.java,用来做字符格式转化
package com.zzstxx.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.stereotype.Component; /** * 过滤器 * @author zxf */ @Component @ServletComponentScan @WebFilter(urlPatterns = "/hello1/*",filterName = "CharacterEncodingFilte") public class CharacterEncodingFilte implements Filter { public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; System.out.println("this is MyFilter,url :" + request.getRequestURI()); arg2.doFilter(arg0, arg1); } public void destroy() { // TODO Auto-generated method stub } }
2、监听器
(1)、创建SpringBoot配置类,并注册监听器
package com.zzstxx; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.zzstxx.listener.ListenerTest; /** * 配置管理类 * @author zxf * */ @Configuration public class WebConfig { public WebConfig() { System.out.println("加载SpringBoot配置WebConfig ........."); // TODO Auto-generated constructor stub } /** * 注册监听器 * @return */ @Bean public ServletListenerRegistrationBean<ListenerTest> servletListenerRegistrationBean() { return new ServletListenerRegistrationBean<ListenerTest>(new ListenerTest()); } }
(2)、创建监听器类
package com.zzstxx.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ListenerTest implements ServletContextListener { public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("监听器初始化..."); } public void contextDestroyed(ServletContextEvent arg0) { System.out.println("监听器销毁..."); } }
3、错误页面处理
当系统报错时,返回到页面的内容通常是一些杂乱的代码段,这种显示对用户来说不友好,因此我们需要自定义一个友好的提示系统异常的页面。在 src/main/resources 下创建 /public/error(路径是固定的,Spring Boot 会在系统报错时将返回视图指向该目录下的文件),在该目录下再创建一个名为 5xx.html 文件,该页面的内容就是当系统报错(错误代码以5开头的)时返回给用户浏览的内容:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>系统错误</title> <link href="/css/index.css" rel="stylesheet"/> </head> <body> <div class="container"> <h2>系统内部错误</h2> </div> </body> </html>
上边处理的 5xx 状态码的问题,接下来解决 404 状态码的问题。
当出现 404 的情况时,用户浏览的页面也不够友好,因此我们也需要自定义一个友好的页面给用户展示。
在 /public/error 目录下再创建一个名为 404.html 的文件:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>访问异常</title> <link href="/css/index.css" rel="stylesheet"/> </head> <body> <div class="container"> <h2>找不到页面</h2> </div> </body> </html>
4、全局异常捕获
package com.zzstxx.exception; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * 全局异常捕获 * @author zxf * */ @ControllerAdvice public class GlobalDefaultExceptionHandler { /** * 处理 Exception 类型的异常 * * @param e * @return */ @ExceptionHandler(Exception.class) @ResponseBody public Map<String, Object> defaultExceptionHandler(Exception e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("code", 500); map.put("msg", e.getMessage()); return map; } }
5、解决跨域的问题:
测试请求链接:http://localhost:8080/fastjson/test2
(1)、方式一
@Configuration public class WebConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { //fastjson为请求路径的一部分 registry.addMapping("/fastjson/**") .allowedOrigins("http://localhost:8088");// 允许 8088 端口访问 } }; } }(2)、方式二
@Configuration public class WebConfig extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/fastjson/**") .allowedOrigins("http://localhost:8088");// 允许 8088 端口访问 } }(3)、 细粒度控制
在 FastJsonController 类中的方法上添加 @CrossOrigin(origins="xx") 注解:
@RequestMapping("/test") @CrossOrigin(origins="http://localhost:8088") public User test() { User user = new User(); user.setId(1); user.setUsername("jack"); user.setPassword("jack123"); user.setBirthday(new Date()); return user; }在使用该注解时,需要注意 @RequestMapping 使用的请求方式类型,即 GET 或 POST。