自己想做网站怎么做,网站开发语音,免费网站模块,做网站哪个最好目录
一、Spring 拦截器
1.1、背景
1.2、实现步骤
1.3、拦截原理
二、 统一url前缀路径
2.1、方法一#xff1a;在系统的配置文件中设置
2.2、方法二#xff1a;在 application.properies 中配置
三、统一异常处理
四、统一返回数据返回格式处理
4.1、背景
4.2、…目录
一、Spring 拦截器
1.1、背景
1.2、实现步骤
1.3、拦截原理
二、 统一url前缀路径
2.1、方法一在系统的配置文件中设置
2.2、方法二在 application.properies 中配置
三、统一异常处理
四、统一返回数据返回格式处理
4.1、背景
4.2、具体实现 一、Spring 拦截器 1.1、背景
在原生的 Spring AOP 中实现统一的拦截的难点在于1.定义拦截规则表达式很难2.在切面类中拿到 HttpSession 比较难如何解决这两个难点呢使用拦截器 1.2、实现步骤
实现一个普通的拦截器关键在于以下两步
实现 HandlerInterceptor 接口重写 preHeadler 方法在方法中编写自己的业务代码。将拦截器添加到配置文件中设置拦截规则。
具体的首先步骤一例如要实现一个用户登录判断就需要创建一个类这里起名叫LoginInterceptor 类实现 HandlerInterceptor 接口重写 preHeadler 方法此方法返回的是以个 boolean 类型如果为 true 表示验证成功可以继续执行后面的流程若是 false 表示验证失败后面的流程就不执行了通过是否可以获取到 Session 信息判断用户是否已经登陆来返回 true 或 false。
a实现 HandlerInterceptor 接口重写 preHeadler 方法如下代码
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;//登录拦截器
public class LoginInterceptor implements HandlerInterceptor {/*** 此方法返回一个 boolean若为 true 表示验证成功否则验证失败后面的流程不能执行了* param request* param response* param handler* return* throws Exception*/Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 用户登录业务判断HttpSession session request.getSession(false);if(session ! null session.getAttribute(userinfo) ! null) {//说明用户已经登陆return true;}//可以调整登录页面或者 返回一个 401/403 没有权限response.sendRedirect(/login.html);return false;}
}b将拦截器添加到配置文件中设置拦截规则
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration
public class AppConfig implements WebMvcConfigurer {Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).addPathPatterns(/**) //连接所有请求值得注意的是这里不能只写一个 *一个 * 表示一级路径.excludePathPatterns(/user/login) //不拦截的 url.excludePathPatterns(/user/reg).excludePathPatterns(/**/*.html); //不拦截所有的页面}
}注意 addPathPatterns表示需要拦截的 URL“**”表示拦截任意⽅法也就是所有⽅法。 excludePathPatterns表示需要排除的 URL。 说明以上拦截规则可以拦截此项⽬中的使⽤ URL包括静态⽂件图⽚⽂件、JS 和 CSS 等⽂件 1.3、拦截原理 二、 统一url前缀路径 2.1、方法一在系统的配置文件中设置
具体的重写 WebMvcConfigurer 接口下的 configurePathMatch 方法例如修改所有请求url添加前缀 /zhangsan 如下代码
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration
public class AppConfig implements WebMvcConfigurer {Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {configurer.addPathPrefix(/zhangsan, c - true);}}2.2、方法二在 application.properies 中配置
例如修改所有请求url添加前缀 /zhangsan如下代码
server.servlet.context-path/zhangsan 三、统一异常处理 统一异常处理是通过如下两个注解结合实现的
ControllerAdvice表示控制器通知类。ExceptionHandler表示异常处理器。
两个结合表示出现异常的时候执行某个通知方法具体的步骤如下
创建一个类标识上 ControllerAdvice在方法上添加 ExceptionHandler;
如下代码
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;ControllerAdvice
ResponseBody
public class MyExHandler {/*** 拦截所有的空指针异常进行统一的数据返回* param e* return*/ExceptionHandler(Exception.class) //这里也可以根据实际情况填写不同的异常public HashMapString, Object nullException(NullPointerException e) {HashMapString, Object reslut new HashMap();reslut.put(code, -1);reslut.put(msg, 空指针异常 e.getMessage());reslut.put(data, null);//这里返回 HashMap 就相当于项前端返回了一个 JSON 格式的数据return reslut;}}四、统一返回数据返回格式处理 4.1、背景
为什么要统一数据返回格式处理例如以下几个原因
方便前端程序员更好的接收和解析后端返回的数据降低约定前后端交互接口的成本按照某种格式实现即可因为所有的接口都是这样返回的。有利于项目的统一数据的维护和修改。4.2、具体实现
统一数据格式返回的实现需要以下两个步骤
创建一个类并添加 ControllerAdvice。实现 ResponseBodyAdvice 接口重写 supports 和 beforeBodyWrite。Ps 1、 supports 方法不用编写业务逻辑而是像一个控制器一样返回 true 则执行 beforeBodyWrite 方法反之则不执行。 2、beforeBodyWrite 方法就是用来实现统一对象的。 具体的如下
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import java.util.HashMap;ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {//将 java 对象转化成 JSON 格式Autowiredprivate ObjectMapper objectMapper;/*** 此方法返回 true 则执行下面的 beforeBodyWrite 方法,反之则不执行* param returnType* param converterType* return*/Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return true;}Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {HashMapString, Object result new HashMap();result.put(code, 200);result.put(msg, );result.put(data,body);// 这里需要进行特殊处理因为 String 在转换的时候报错if(body instanceof String) {try {return objectMapper.writeValueAsString(result);} catch (JsonProcessingException e) {e.printStackTrace();}}return result;}
}代码中为什么要进行特殊处理?最易出错
在 java 程序中 String 是一个最特殊的类型既不是基础类型也不是对象并且在重写方法时也很特殊除了 String 其他的都是用一个格式化工具而 String 用的是自己的一套格式化工具因此在转换成 HashMap 的时候还没有被加载好而其他的转换器已经加载好了最后就会引发如下异常 因此就要判断 body 是否为 String 若为 String 类型就要进行特殊处理使用 JSON 的writeValueAsString 方法将 Java 对象转换成 JSON 格式再返回。 文章转载自: http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.prplf.cn.gov.cn.prplf.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn