我试图使用Apache camel从Web服务中提取数据。但是对于相同的API,我得到了正确的JSON响应以及不同过滤条件的异常。
例如:
http://student.com/rest/student-id/44
这里44是有效的student-Id
将为我提供核心JSON响应和无CXF异常
但是当我点击一个无效的学生ID时,请说' 1n23', webserivce将返回正确的错误JSON
{
"error": "No record found."
}
但是在骆驼中我得到了这个例外
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]
这是我的骆驼路线
<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>
RequestProcesor
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>
在StdErrorHandler
中public void errorHandler(final Exchange exchange, @Body String payload ){
System.out.println(payload);
}
在这里,我只会收到我在
中设置的虚拟身体exchange.getOut().setBody("Dummy Body");
我如何获得响应有效负载。