提交 18eb38d1 编写于 作者: F fengyongwei

修改网关的bug

上级 cca2e72f
......@@ -16,6 +16,7 @@ import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -73,7 +74,7 @@ public class FilterPre extends ZuulFilter {
}
@Override
public Object run() {
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
String uri = RequestContext.getCurrentContext().getRequest().getServletPath();
logger.info("请求地址" + uri);
......@@ -96,20 +97,18 @@ public class FilterPre extends ZuulFilter {
}
} catch (BaseException e) {
logger.error("系统异常", e.getMessage());
ctx.setSendZuulResponse(false);
resp(ctx, e.getMessage(), e.getCode());
return null;
logger.error("系统异常", e);
throw new ZuulException(e, e.getCode(), e.getMessage());
}
// 参数封装
try {
ctx.setRequest(requestWrapper(request, userNo));
} catch (IOException e) {
return null;
} catch (Exception e) {
logger.error("封装参数异常", e.getMessage());
ctx.setSendZuulResponse(false);
resp(ctx, "系统异常,请重试");
throw new ZuulException(e, ResultEnum.ERROR.getCode(), e.getMessage());
}
return null;
}
/**
......@@ -180,16 +179,6 @@ public class FilterPre extends ZuulFilter {
};
}
private void resp(RequestContext ctx, String msg) {
ctx.getResponse().setCharacterEncoding("UTF-8");
ctx.setResponseBody(JSONUtil.toJSONString(Result.error(msg)));
}
private void resp(RequestContext ctx, String msg, int code) {
ctx.getResponse().setCharacterEncoding("UTF-8");
ctx.setResponseBody(JSONUtil.toJSONString(Result.error(code, msg)));
}
@SuppressWarnings("unchecked")
private static TreeMap<String, Object> getParamMap(HttpServletRequest request) {
TreeMap<String, Object> paramMap = new TreeMap<>();
......
package com.roncoo.education.server.gateway.controller;
import com.roncoo.education.util.base.Result;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
@RestController
public class HandlerController implements ErrorController {
/**
* 出异常后进入该方法,交由下面的方法处理
*/
@Override
public String getErrorPath() {
return "/error";
}
@RequestMapping("/error")
@ResponseStatus(HttpStatus.OK)
public Result<String> error() {
RequestContext ctx = RequestContext.getCurrentContext();
ZuulException e = (ZuulException) ctx.getThrowable();
return Result.error(e.nStatusCode, e.errorCause);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册