我想将响应主体设置为异常消息,响应主体始终为空。 响应主体在过滤器的末尾是好的,但之后会被破坏。 问题是其他的Zuul过滤器,还是我缺少的东西? 我有以下ZullErrorFilter
Button
并且以下Zuul过滤器处于活动状态:
@Component
public class ErrorFilter extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(ErrorFilter.class);
private static final String THROWABLE_KEY = "throwable";
private static final int FILTER_ORDER = -1;
@Override
public String filterType() {
return ERROR_TYPE;
}
@Override
public int filterOrder() {
return FILTER_ORDER;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
final RequestContext context = RequestContext.getCurrentContext();
final Object throwable = context.get(THROWABLE_KEY);
if (throwable instanceof ZuulException) {
final ZuulException zuulException = (ZuulException) throwable;
Throwable cause = zuulException.getCause();
LOG.error("Zuul failure detected: " + cause.getMessage());
// remove error code to prevent further error handling in follow up filters
context.remove(THROWABLE_KEY);
System.out.println(cause.getMessage());
context.setResponseBody(cause.getMessage());
context.getResponse().setContentType("application/json");
System.out.println(context.getResponseBody());
if (causeIsIOError(cause)) {
context.setResponseStatusCode(409);
} else if (causeIsAuthorizationError(cause)) {
context.setResponseStatusCode(401);
}
}
return null;
}
private boolean causeIsIOError(Throwable cause) {
return cause instanceof InvalidTokenPayloadException
|| cause instanceof InvalidResponseBodyException;
}
public boolean causeIsAuthorizationError(Throwable cause) {
return cause instanceof InvalidJWTTokenException ||
cause instanceof NoPermissionForResourceException ||
cause instanceof MissingAuthorizationHeaderException;
}
}
编辑: 删除了这一行:
{
"error": [
{
"class": "org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter",
"order": 0,
"disabled": false,
"static": true
},
{
"class": "com.example.security.auth.filters.ErrorFilter",
"order": -1,
"disabled": false,
"static": true
}
],
"post": [
{
"class": "com.example.security.auth.filters.JwtPostFilter",
"order": 999,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter",
"order": 1000,
"disabled": false,
"static": true
}
],
"pre": [
{
"class": "com.example.security.auth.filters.JwtPreFilter",
"order": 4,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.pre.Servlet30WrapperFilter",
"order": -2,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.pre.ServletDetectionFilter",
"order": -3,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter",
"order": 5,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter",
"order": 1,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter",
"order": -1,
"disabled": false,
"static": true
}
],
"route": [
{
"class": "org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter",
"order": 100,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter",
"order": 10,
"disabled": false,
"static": true
},
{
"class": "org.springframework.cloud.netflix.zuul.filters.route.SendForwardFilter",
"order": 500,
"disabled": false,
"static": true
}
]
}
现在正文可以了,但是不能正确处理异常。它只是在响应中抛出ZuulFilterExceptions。状态码始终为500。