我使用Camel -rest连接到外部API并验证json模式,应用程序按预期工作,但是,在调用时没有响应回来,我只能通过Camel内部的日志看到它,如下所示:
INFO [route3] (vert.x-worker-thread-0) HTTP Response: [{"description":"Winter fruit","name":"Apple"},{"description":"Tropical fruit","name":"Pineapple"}]
和任何旋度只返回200 OK
这是我的代码:
public class Validator extends RouteBuilder {
private static final String REST_ENDPOINT_ECHO= "{{rest.endpoint}}?bridgeEndpoint=true" +
"©Headers=true" +
"&connectionClose=true";
@Override
public void configure() throws Exception {
onException(JsonValidationException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.setHeader(Exchange.CONTENT_TYPE, simple("text/plain"))
.log("Body: ${body}, Header: ${headers}")
.setBody(simple("Error: Invalid JSON Schema"));
rest()
.post("/{{rest.endpoint.subpath}}").enableCORS(true).route()
.to("direct:validator")
.endRest();
from("direct:echoServiceUrl")
.to("json-validator:file:/deployments/schema.json")
.to("http://" + REST_ENDPOINT_ECHO)
.log("HTTP Response: " + "${body}")
.convertBodyTo(String.class)
.end();
Http组件返回CachedOutputStream所以当你记录它这个流对象被消耗,你得到一个空的响应。这样,流对象将在接下来的步骤中存储。第一个解决方案,你可以设置流缓存到你的代码,像这样
.to("https://anyurl.com")
.streamCaching()
.log("my body is ${body}");
否则你必须删除日志行
.log("HTTP Response: " + "${body}")