Spring cloud 网关对返回类型全部设置为json以及对文件格式不处理

package com.**.saas.gateway.filter;
/**
 * 返回值输出过滤器,用于处理返回值
 *
 * @author: ***
 * @date: 2019/6/19 15:18
 * @version: 1.0
 */
@Component
public class ResponseFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext context = getCurrentContext();
        return !context.getRequest().getRequestURI().toString().contains("getDownloadExcel");
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context = getCurrentContext();// 获取上下文
        context.addZuulResponseHeader("Content-Type", "application/json;charset=UTF-8");
        InputStream inputStream = context.getResponseDataStream();
        String responseBody;
        try {
            responseBody = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
        } catch (IOException e) {
            throw ExceptionUtil.buildZuulException(ResultEnum.SYSTEM_ERROR);
        }
        if (context.getResponseStatusCode() != 200) {// zuul网关请求状态
            ZuulErrorBean zuulErrorBean = JsonUtil.json2Obj(responseBody, ZuulErrorBean.class);
            if (zuulErrorBean != null) {
                throw ExceptionUtil.buildZuulException(ResultDTO.fail(zuulErrorBean.getStatus(), zuulErrorBean.getError()));
            }
            return null;
        }
        context.setResponseBody(responseBody);
        return null;
    }
}

你可能感兴趣的:(spring,cloud)