我通过HTTP-POST向我的Camel-Spark-REST组件发送无效 JSON主体。在我的Camel路线上,JSON主体将从JSON转换(编组)到具有Jackson库的Java对象。编组失败,因为JSON主体无效。使用camel doTry / doCatch元素,我尝试捕获异常并由我自己处理它们。这看起来像这样:
$(document).on("click", ".panel", function(e){
var check = $(this).find(':checkbox');
if(check.prop('checked') == true){
check.prop('checked', false);
$(this).css('opacity', '1');
} else {
check.prop('checked', true);
$(this).css('opacity', '0.2');
}
}
如果存在无效的JSON主体,杰克逊封送器将抛出JsonParseException。如上所示,我尝试捕获JsonParseException异常,它应该在我的控制台rest("/v1/users")
.consumes("application/json")
.produces("application/json")
.post("/insert")
.to("direct:restInput");
from("direct:restInput")
.doTry()
.marshal().json(JsonLibrary.Jackson)
[...]
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
//throw new JsonParseException(null, "");
}
})
[...]
.doCatch(JsonParseException.class)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println("JsonParseException occured");
}
})
.doCatch(Exception.class)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println("Exception occured");
}
})
.end()
.marshal();
上输出。
但异常是没有捕捉,我在我的控制台上获得了完整的堆栈跟踪:
JsonParseException occured
根据我的测试,这发生在Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[route4 ] [route4 ] [spark-rest://post:v1/users/insert?accept=application%2Fjson ] [ 42]
[route4 ] [restBinding4 ] [ ] [ 39]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
com.fasterxml.jackson.core.JsonParseException: Unexpected character (':' (code 58)): was expecting comma to separate Array entries
at [Source: java.io.ByteArrayInputStream@1aa6eb2; line: 23, column: 17]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702) ~[jackson-core-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558) ~[jackson-core-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:456) ~[jackson-core-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:761) ~[jackson-core-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapArray(UntypedObjectDeserializer.java:594) ~[jackson-databind-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:510) ~[jackson-databind-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789) ~[jackson-databind-2.8.3.jar:2.8.3]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2899) ~[jackson-databind-2.8.3.jar:2.8.3]
at org.apache.camel.component.jackson.JacksonDataFormat.unmarshal(JacksonDataFormat.java:172) ~[camel-jackson-2.18.0.jar:2.18.0]
at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.component.rest.RestConsumerBindingProcessor.process(RestConsumerBindingProcessor.java:189) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.component.sparkrest.CamelSparkRoute.handle(CamelSparkRoute.java:46) [camel-spark-rest-2.18.0.jar:2.18.0]
at spark.RouteImpl$1.handle(RouteImpl.java:58) [spark-core-2.3.jar:?]
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162) [spark-core-2.3.jar:?]
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61) [spark-core-2.3.jar:?]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544) [jetty-io-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.19.v20160908.jar:9.2.19.v20160908]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
部分。如果我发送一个有效的JSON体并在第一个处理器中手动抛出JsonParseException(请参阅注释行),那么将捕获该异常。 .marshal().json(JsonLibrary.Jackson)
catch块也永远不会被调用,因此可以排除它,我试图捕获“错误”异常。有没有机会从杰克逊的封送者那里抓住这个例外,或者是否有一个骆驼组件可以检查JSON身体,如果它形成得很好,我可以把它放在封送者面前?
答案 0 :(得分:1)
可能会在端点中抛出异常,而不是在编组步骤中抛出异常。我不知道spart-rest端点,但是对于Jetty端点和REST DSL,Jetty端点本身会抛出编组错误。除了堆栈跟踪,Camel还应记录消息历史记录。您可以在历史记录中验证是否执行了编组步骤。
2017-01-19 09:11:28,040 | ERROR | qtp762554626-172 | DefaultErrorHandler | 75 - org.apache.camel.camel-core - 2.16.3 | | Failed delivery for (MessageId: ID-Ralfs-MacBook-Pro-local-60691-1484813099793-0-2 on ExchangeId: ID-Ralfs-MacBook-Pro-local-60691-1484813099793-0-1). Exhausted after delivery attempt: 1 caught: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: java.io.ByteArrayInputStream@49828827; line: 33, column: 6]
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[route1 ] [route1 ] [jetty:http://0.0.0.0:8282/some/path?httpMethodRestrict=PUT ] [ 15]
[route1 ] [restBinding1 ] [ ] [ 12]
Exchange
---------------------------------------------------------------------------------------------------------------------------------------
Exchange[
Id ID-Ralfs-MacBook-Pro-local-60691-1484813099793-0-1
ExchangePattern InOut
[..]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: java.io.ByteArrayInputStream@49828827; line: 33, column: 6]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581)[55:com.fasterxml.jackson.core.jackson-core:2.6.3]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533)[55:com.fasterxml.jackson.core.jackson-core:2.6.3]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:462)[55:com.fasterxml.jackson.core.jackson-core:2.6.3]
[..]
如果在端点上确实抛出了异常,那么自定义错误处理的选项取决于您想要执行的错误处理的类型。
正如您自己建议的那样,您可以使用全局onException
处理程序来使用处理器进行错误处理。
如果您只想控制客户端收到的错误消息和http响应代码的类型,那么提供您自己的http绑定或请求/响应处理策略可能更容易。我再也不知道spark-rest端点,但是对于Jetty,你会提供你自己的http绑定实现,如下所示:
public class JettyNoStacktraceHttpBinding extends JettyRestHttpBinding {
private static final int BAD_REQUEST = 400;
@Override
public void doWriteExceptionResponse(Throwable exception, javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
if (exception instanceof JsonParseException) {
response.setStatus(BAD_REQUEST);
response.setContentType("text/plain");
PrintWriter pw = response.getWriter();
pw.print(exception.getMessage());
pw.flush();
}
}
}
<restConfiguration contextPath="/some/path" component="jetty" scheme="http" host="0.0.0.0" port="8282" bindingMode="json">
<endpointProperty key="httpBindingRef" value="jettyNoStackTraceHTTPBinding" />
<!-- ... -->
</restConfiguration>