问题
from("somegcpchannel").
.choice()
.when().jsonpath(myClassObject.getJsonPathExpressions().get(),true)//true will suppress exception if the path does not exist"
The problem with this camel jsonpath component is that if myClassObject.getJsonPathExpression() is null or empty it throws an exception.Hence I am forced to put some dummy json path to get it working.
How can I first check if the myClassObject.getJsonPathExpressions() if not null only then process the json path expression.All in one statement if possible(not nested choice / when).It is weird that json path component of camel does not do the null check
回答1:
This is a pure Java problem. You want to use a value that is stored in an instance object of your RouteBuilder class to construct a Camel Route.
myClassObject.getJsonPathExpressions().get()
When myClassObject.getJsonPathExpressions()
returns null
then the following .get()
method obviously throws a NPE. Therefore the Camel route cannot be constructed.
Sidenote: this is a static value anyway. The value returned from the object is used while constructing the route and will never change while the application is running.
Since it is a pure Java problem, you cannot use Camel to solve it. You have to solve it in Java.
The most simple solution (as you already stated) is to always provide a value. To make your Route happy, just add a method to your RouteBuilder class that returns the object value if present or a sensible default.
private String getJsonPathExpression() {
if (whatever checks are needed) {
return myClassObject.getJsonPathExpressions().get(); // value present
} else {
return "default JsonPath that works" // no value present
}
}
And then use this method in your route instead of using the object directly
.when().jsonpath(getJsonPathExpression(),true)
This way you simply hide all the value checking stuff in the method.
来源:https://stackoverflow.com/questions/62204589/when-jsonpathmyclass-getjsonpathexpressions-get-true-not-working-if-jso