admin 管理员组文章数量: 887021
2024年2月19日发(作者:我的世界服务器创建网站)
源码如下package ;import ist;import ;import rentHashMap;import rvletRequest;import rvletResponse;import Filter;import sponseFilter;import tWrapperFilter;import gFilter;public class EatRunner{//静态写死过滤器private ConcurrentHashMap> hashFiltersByType = new ConcurrentHashMap>(){{put("pre",new ArrayList(){{add(new RequestWrapperFilter());}});put("route",new ArrayList(){{add(new RoutingFilter());}});put("post",new ArrayList(){{add(new SendResponseFilter());}});}};public void init(HttpServletRequest req, HttpServletResponse resp) {RequestContext ctx = rentContext();uest(req);ponse(resp);}public void preRoute() throws Throwable {runFilters("pre");}public void route() throws Throwable{runFilters("route");
import mplate;import Filter;import tContext;public class RoutingFilter extends EatuulFilter{@Overridepublic String filterType() {// TODO Auto-generated method stubreturn "route";}@Overridepublic int filterOrder() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void run(){RequestContext ctx = rentContext();RequestEntity requestEntity = uestEntity();RestTemplate restTemplate = new RestTemplate();ResponseEntity responseEntity = ge(requestEntity,byte[].class);ponseEntity(responseEntity);}}这个是postFilters,将ResponseEntity输出即可package ;import ;import ;import tOutputStream;import rvletResponse;import aders;import seEntity;import Filter;import tContext;
public class SendResponseFilter extends EatuulFilter{@Overridepublic String filterType() {return "post";}@Overridepublic int filterOrder() {return 1000;}@Overridepublic void run() {try {addResponseHeaders();writeResponse();} catch (Exception e) {tackTrace();}}private void addResponseHeaders() {RequestContext ctx = rentContext();HttpServletResponse servletResponse = ponse();ResponseEntity responseEntity = ponseEntity();HttpHeaders httpHeaders = ders();for(
public static RequestContext getCurrentContext() {RequestContext context = ();return context;}public HttpServletRequest getRequest() {return (HttpServletRequest) get("request");}public void setRequest(HttpServletRequest request) {put("request", request);}public HttpServletResponse getResponse() {return (HttpServletResponse) get("response");}public void setResponse(HttpServletResponse response) {set("response", response);}public void setRequestEntity(RequestEntity requestEntity){set("requestEntity",requestEntity);}public RequestEntity getRequestEntity() {return (RequestEntity) get("requestEntity");}public void setResponseEntity(ResponseEntity responseEntity){set("responseEntity",responseEntity);}public ResponseEntity getResponseEntity() {return (ResponseEntity) get("responseEntity");}public void set(String key, Object value) {if (value != null)put(key, value);elseremove(key);}
版权声明:本文标题:java自己实现网关_自己动手写一个服务网关 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1708355123h521131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论