如何在Spring Cloud Gateway中转发POST方法

时间:2018-10-19 08:43:46

标签: spring-cloud-gateway

我在项目中使用了微服务和Spring云网关

因为,网关服务是微服务中服务之前的过滤器请求。现在,我可以通过网关传递GET api,但是使用POST api不能做到

这是我的GETs api,我可以通过网关

@Component
public class RouteServiceForwardingFilter implements GatewayFilter {
    private static final Logger log = LoggerFactory.getLogger(RouteServiceForwardingFilter.class);

    private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();

    @Autowired
    private ServiceDiscoveryApis serviceDiscoveryApis;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        try { 
            String forwardUrl = "http://my-service:8080/api/info";
            exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, new URI(forwardUrl));
        return chain.filter(exchange);
    } catch (CoreServerRuntimeException e) {
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        DataBuffer buf = dataBufferFactory.wrap(CommonUtils.toJsonString(ResponseData.createError(e.getErrCode(), e.getErrMsg())).getBytes(StandardCharsets.UTF_8));
        return response.writeWith(Flux.just(buf));

    } catch (Exception e) {
        return Mono.empty();
    }
    }
}

0 个答案:

没有答案