简体   繁体   English

Spring集成流响应HTTP 400

[英]Spring integration flow respond with HTTP 400

I have a spring integration flow that expects a "routingkey" message header. 我有一个Spring集成流程,需要一个“routingkey”消息头。 If this is not present I'd like to send a HTTP 400 response back to the client. 如果不存在,我想将HTTP 400响应发送回客户端。 How can I do that? 我怎样才能做到这一点? Below you will see the determineRoutingKey method is where I can determine whether the routing key was passed as a header. 您将在下面看到determineRoutingKey方法,我可以确定路由密钥是否作为标头传递。

@Bean
public IntegrationFlow webToRabbit(RabbitTemplate amqpTemplate)
{
    return IntegrationFlows
            .from
            (
                Http.inboundGateway("/tunnel")
                    .replyTimeout(Integer.valueOf(timeout))
                    .mappedRequestHeaders("*")
                    .mappedResponseHeaders("*")
            )
            .log()
            .handle
            (
                Amqp.outboundGateway(amqpTemplate)
                    .exchangeName(exchangeName)
                    .routingKeyFunction(f->determineRoutingKey(f))
                    .mappedRequestHeaders("*")
                    .mappedReplyHeaders("*")
            )
            .log()
            .bridge(null)
            .get();
}

private String determineRoutingKey(Message<?> message) 
{
    MessageHeaders headers = message.getHeaders();
    if(headers.containsKey(HEADER_ROUTINGKEY))
    {
        String routingKey = Objects.toString(headers.get(HEADER_ROUTINGKEY));
        log.debug("Using routing key: " + routingKey);
        return routingKey;
    }
    else
    {
        log.error("Headers found: " + Objects.toString(headers));

        //Here I get an exception stating that MessageHeaaders is immutable
        message.getHeaders().put(HttpHeaders.STATUS_CODE, HttpStatus.BAD_REQUEST);
        return null;
    }
}

Thanks in advance for the help. 先谢谢您的帮助。

There is a logic like: 有一个逻辑:

private HttpStatus resolveHttpStatusFromHeaders(MessageHeaders headers) {
    Object httpStatusFromHeader = headers.get(org.springframework.integration.http.HttpHeaders.STATUS_CODE);
    return buildHttpStatus(httpStatusFromHeader);
}

So, what you need is just regular (string with error message?) reply and an appropriate HttpStatus.BAD_REQUEST as a http_statusCode header. 所以,你需要的是只需定期(字符串错误讯息?)回复和适当HttpStatus.BAD_REQUEST作为http_statusCode头。

No reason in any exceptions. 在任何例外中都没有理由。

UPDATE UPDATE

Since you are in the middle of the flow and it is still far away from the reply, you can stick with that exception approach. 由于您处于流程的中间并且距离回复还很远,因此您可以坚持使用该异常方法。

What you have to do there to return proper 400 response is an errorChannel on the Http.inboundGateway() and some simple transformer to returns a Message with an appropriate HttpHeaders.STATUS_CODE header: 你所要做的有返回正确的响应400是errorChannelHttp.inboundGateway()和一些简单的变压器回报Message用适当HttpHeaders.STATUS_CODE头:

Http.inboundGateway("/tunnel")
    .errorChannel("httpErrorFlow.input")
...

@Bean
public IntegrationFlow httpErrorFlow() {
    return f -> f
            .<RuntimeException, Message<?>>transform(payload -> {
                if (payload.getCause() instanceof MyRoutingKeyException) {
                    return MessageBuilder.withPayload("Bad routing key")
                            .setHeader(HttpHeaders.STATUS_CODE, HttpStatus.BAD_REQUEST)
                            .build();
                }
                throw payload;
            });
}

But again: you can't just throw an exception. 但同样:你不能只是抛出异常。

That is 那是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM