Apache Camel REST DSL-验证请求有效载荷并返回错误响应 [英] Apache Camel REST DSL - Validating Request Payload and return error response
问题描述
我正在使用"CamelHttpTransportServlet"公开一个休息服务,该服务接收订单并将其放置在jms队列中.该代码可以在快乐路径上正常工作,并返回200响应. 我已经编写了Processor来验证输入的JSON,并根据输入设置http_response_code.
I am exposing a rest service using "CamelHttpTransportServlet" that receive orders and place in jms queue. The code works fine on happy path and returns 200 response. I have written Processor to validate the input JSON, and set http_response_code based on the input.
问题是-对于无效请求,尽管设置了失败响应代码-设置为400,流程继续到下一个路由,并将数据推送到队列,而不是将400响应发送回调用应用程序.
The issue is - for invalid requests though failure response code - 400 is set, the flow continues to the next route and pushes the data to the queue instead of sending the 400 response back to the calling app.
rest("/ordermanagement")
.post("/order").to("direct:checkInput");
from("direct:checkInput")
.process(new Processor() {
@Override
public void process(final Exchange exchange) throws Exception {
String requestBody = exchange.getIn().getBody(String.class);
if(requestBody == "" || requestBody== null) {
exchange.getIn().setBody("{ "error": Bad Request}");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
}
}
})
.to("direct:sendToQ");
from("direct:sendToQ")
.to("jms:queue:orderReceiver")
.log("Sent to JMS");
有人可以告知这里缺少什么并提供样本吗?
Can someone advise what is missing here and provide a sample if possible?
尝试实施onException方法:
rest("/ordermanagement")
.post("/order").to("direct:checkInput");
onException(CustomException.class).handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, code)
.setBody(jsonObject);
from("direct:checkInput")
.process(new Processor() {
@Override
public void process(final Exchange exchange) throws Exception {
String requestBody = exchange.getIn().getBody(String.class);
if(requestBody == "" || requestBody== null) {
throw CustomException(code, jsonObject)
}
}
})
.to("direct:sendToQ");
from("direct:sendToQ")
.to("jms:queue:orderReceiver")
.log("Sent to JMS");
但是我不知道如何将参数-code,jsonObject从处理器传递到onException块.
However I could not figure out how to pass the parameters - code,jsonObject from processor to onException block.
对此有任何帮助吗?这可行吗?
Any help on this? Is this feasible?
推荐答案
这里是一种方法.您可以使用choice
Here is one way to do it. You can use choice
rest("/ordermanagement")
.post("/order").to("direct:checkInput");
from("direct:checkInput")
.process(exchange -> {
String requestBody = exchange.getIn().getBody(String.class);
if(requestBody == null || requestBody.equals("")) {
exchange.getIn().setBody("{ "error": Bad Request}");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
}
})
.choice()
.when(exchange -> {
Object header = exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE);
return header != null && header.equals(400);
})
.stop()
.otherwise()
.to("direct:sendToQ")
.endChoice();
from("direct:sendToQ")
.to("jms:queue:orderReceiver")
.log("Sent to JMS");
这篇关于Apache Camel REST DSL-验证请求有效载荷并返回错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!