标签:href ica 返回 示例 ppi run ORC OLE auth
本文转载自:https://blog.csdn.net/tianyaleixiaowu/article/details/77893822
上一篇介绍了java网关Zuul的简单使用,进行请求路由转发和过滤器的基本操作。
这一篇主要看一下它的过滤器Filter的工作流程及异常处理。
首先看到Filter的四个方法,FilterType,filterOrder,shouldFilter,run。
这个不多说,试一下就知道了。
package com.tianyalei.testzuul; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component public class AccessFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(AccessFilter.class); @Override public String filterType() { //前置过滤器 return "pre"; } @Override public int filterOrder() { //优先级,数字越大,优先级越低 return 0; } @Override public boolean shouldFilter() { //是否执行该过滤器,true代表需要过滤 return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString()); //获取传来的参数accessToken Object accessToken = request.getParameter("accessToken"); if(accessToken == null) { log.warn("access token is empty"); //过滤该请求,不往下级服务去转发请求,到此结束 ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); ctx.setResponseBody("{\"result\":\"accessToken为空!\"}"); ctx.getResponse().setContentType("text/html;charset=UTF-8"); return null; } //如果有token,则进行路由转发 log.info("access token ok"); //这里return的值没有意义,zuul框架没有使用该返回值 return null; } }
第二个前置过滤器
package com.tianyalei.testzuul; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component public class SecondFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(SecondFilter.class); @Override public String filterType() { //前置过滤器 return "pre"; } @Override public int filterOrder() { //优先级,数字越大,优先级越低 return 1; } @Override public boolean shouldFilter() { //是否执行该过滤器,true代表需要过滤 return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info("second过滤器"); return null; } }
后置过滤器
package com.tianyalei.testzuul; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class PostFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(PostFilter.class); @Override public String filterType() { //后置过滤器 return "post"; } @Override public int filterOrder() { //优先级,数字越大,优先级越低 return 0; } @Override public boolean shouldFilter() { //是否执行该过滤器,true代表需要过滤 return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); log.info("进入post过滤器"); System.out.println(ctx.getResponseBody()); ctx.setResponseBody("post后置数据"); int i = 1 / 0; return null; } }
异常过滤器
package com.tianyalei.testzuul; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class ErrorFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(ErrorFilter.class); @Override public String filterType() { //异常过滤器 return "error"; } @Override public int filterOrder() { //优先级,数字越大,优先级越低 return 0; } @Override public boolean shouldFilter() { //是否执行该过滤器,true代表需要过滤 return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); log.info("进入异常过滤器"); System.out.println(ctx.getResponseBody()); ctx.setResponseBody("出现异常"); return null; } }
定义好之后,直接测试看看
package com.tianyalei.testzuul; import org.springframework.boot.autoconfigure.web.ErrorController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class ErrorHandlerController implements ErrorController { /** * 出异常后进入该方法,交由下面的方法处理 */ @Override public String getErrorPath() { return "/error"; } @RequestMapping("/error") public String error() { return "出现异常"; } }
在"/error"方法里返回你想给客户端返回的值即可。
标签:href ica 返回 示例 ppi run ORC OLE auth
原文地址:https://www.cnblogs.com/wpcnblog/p/9054837.html