我试图拉数据从一个Web服务使用Apache Camel 。但是对于相同的API,我得到了正确的JSON响应以及不同过滤条件的异常。
例如:
http://student.com/rest/student-id/44
这里44是有效学生ID
我将给予我正确的JSON响应和无CXF异常
但是,当我用一个无效的学生ID命中,说'1n23',webservivce将返回一个正确的错误JSON
{
"error": "No record found."
}
但在 Camel 中,
org.apache.cxf.transport.http.HTTPException: HTTP response '404: null' when communicating with http://student.com/rest/student-id/1n23
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1581) ~[cxf-rt-transports-http-3.0.4.jar:3.0.4]
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream$1.run(HTTPConduit.java:1155) ~[cxf-rt-transports-http-3.0.4.jar:3.0.4]
at org.apache.cxf.workqueue.AutomaticWorkQueueImpl$3.run(AutomaticWorkQueueImpl.java:428) [cxf-core-3.0.4.jar:3.0.4]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_102]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_102]
at org.apache.cxf.workqueue.AutomaticWorkQueueImpl$AWQThreadFactory$1.run(AutomaticWorkQueueImpl.java:353) [cxf-core-3.0.4.jar:3.0.4]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_102]
这是我的 Camel 路线
<route streamCache="true" id="StudentService">
<from uri="servlet:{{student.service.url.relative}}" />
<to uri="bean:RequestProcessor?method=buildReq" />
<to uri="log:camelRouteLogger?showHeaders=true" />
<recipientList>
<simple>cxf://?dataFormat=MESSAGE&loggingFeatureEnabled=true</simple>
</recipientList>
<to uri="log:camelRouteLogger?showHeaders=true" />
</route>
请求处理器
public void buildReq(final Exchange exchange) {
try {
final HttpMessage httpMessage = exchange.getIn(HttpMessage.class);
String serviceUrl = StudentUrlWithStudentCode;
exchange.setOut(new DefaultMessage());
final Map<String, Object> map = new HashMap<String, Object>();
map.put(Message.HTTP_REQUEST_METHOD, "GET");
exchange.getOut().setHeader(Client.REQUEST_CONTEXT, map);
exchange.getOut().setHeader(Exchange.DESTINATION_OVERRIDE_URL, serviceUrl);
exchange.getOut().setHeader("API-KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
exchange.getOut().setHeader("Accept", "application/json");
exchange.getOut().setBody("-");
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
编辑我在路由中添加了以下内容
<onException>
<exception>org.apache.cxf.transport.http.HTTPException</exception>
<handled>
<constant>true</constant>
</handled>
<bean ref="stdErrorHandler" method="errorHandler"/>
</onException>
在标准错误中
public void errorHandler(final Exchange exchange, @Body String payload ){
System.out.println(payload);
}
在这里,我将只收到我设置的假人身体
exchange.getOut().setBody("Dummy Body");
我将如何获得响应有效载荷。
1条答案
按热度按时间pepwfjgg1#
在HTTPConduit中,我发现了以下评论: