我有一个非常类似于camel \ examples \ camel-example-cxf \ src \ main \ java \ org \ apache \ camel \ example \ cxf \ jaxrs(在camel git项目中)的示例代码的路由。 / p>
所以我有一条路线:
public void configure() {
from(REST_ENDPOINT_URI)
.process(new MappingProcessor(new MyImpl()));
}
// Mapping the request to object's invocation
private static class MappingProcessor implements Processor {
private Class<?> beanClass;
private Object instance;
public MappingProcessor(Object obj) {
beanClass = obj.getClass();
instance = obj;
}
public void process(Exchange exchange) throws Exception {
String operationName = exchange.getIn().getHeader(CxfConstants.OPERATION_NAME, String.class);
Method method = findMethod(operationName, exchange.getIn().getBody(Object[].class));
try {
Object response = method.invoke(instance, exchange.getIn().getBody(Object[].class));
exchange.getOut().setBody(response);
} catch (InvocationTargetException e) {
throw (Exception)e.getCause();
}
}
private Method findMethod(String operationName, Object[] parameters) throws SecurityException, NoSuchMethodException {
return beanClass.getMethod(operationName, getParameterTypes(parameters));
}
private Class<?>[] getParameterTypes(Object[] parameters) {
if (parameters == null) {
return new Class[0];
}
Class<?>[] answer = new Class[parameters.length];
int i = 0;
for (Object object : parameters) {
answer[i] = object.getClass();
i++;
}
return answer;
}
}
我有一个资源只有一个PUT操作,并且在其中我出于各种原因抛出WebApplicationExceptions,例如404资源未找到时,即:
Response r = Response.status(Response.Status.NOT_FOUND).entity("Resource not found").build();
throw new WebApplicationException(r);
在junit CXF测试中,这很好用。但是当我的驼峰路线在上面并且正在运行并且在一个带有curl的PUT请求中发射时,由于某种原因我仍然得到200 OK响应,即使我肯定会抛出404.
为什么会这样?这是camel抛出的堆栈跟踪(只是为了澄清,我没有创建任何空指针异常):
17:14:09,608 WARN org.apache.cxf.common.logging.LogUtils:443 doLog() - Interceptor for {http://REST.myhost.com/}MyImpl has thrown exception, unwinding now
java.lang.NullPointerException
at org.apache.cxf.transport.http.AbstractHTTPDestination$1.cacheInput(AbstractHTTPDestination.java:273)
at org.apache.cxf.transport.http.AbstractHTTPDestination.cacheInput(AbstractHTTPDestination.java:529)
at org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:541)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.flushHeaders(JettyHTTPDestination.java:380)
at org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:534)
at org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:711)
at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:47)
at com.ctc.wstx.sw.EncodingXmlWriter.flushBuffer(EncodingXmlWriter.java:697)
at com.ctc.wstx.sw.EncodingXmlWriter.flush(EncodingXmlWriter.java:171)
at com.ctc.wstx.sw.BaseStreamWriter.flush(BaseStreamWriter.java:259)
at org.apache.cxf.binding.xml.interceptor.XMLFaultOutInterceptor.handleMessage(XMLFaultOutInterceptor.java:85)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:113)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:331)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.phase.PhaseInterceptorChain.resume(PhaseInterceptorChain.java:241)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:78)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.serviceRequest(JettyHTTPDestination.java:355)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:319)
at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:72)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1074)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1010)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:193)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handleAsync(Server.java:405)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:490)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:642)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:627)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:51)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:744)
修改 即使我没有抛出任何异常,即我的PUT处理程序只返回200 OK并且没有抛出异常,一旦控制离开上述处理器,我仍然得到相同的堆栈跟踪。
答案 0 :(得分:0)
这是在tomcat BTW中运行的。我改变了我的网址:
from("cxfrs:/rest" +
"?resourceClasses=org.apache.camel.example.cxf.jaxrs.resources.BookStoreImpl").
而不是:
from("cxfrs:/http://localhost:9090/rest" +
"?resourceClasses=org.apache.camel.example.cxf.jaxrs.resources.BookStoreImpl").
然后通过端口8080访问它。