You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by grzechol <gr...@outlook.com> on 2016/05/20 11:12:49 UTC

Inject multiple @JsonPath parameters to bean method

Hi guys,

I have a problem injecting multiple @JsonPath arguments using bean binding.

Here is my sample json:
{"key1":"val1","key2":"val2"}

My bean method processing that JSON:
@Handler
public void process(@JsonPath("$.key1") String value1, @JsonPath("$.key2")
String value2) {
    System.out.println("Got key1: " + value1);
    System.out.println("Got key2: " + value2);
}

And my route:
<route id="test">
    <from uri="timer://foo?period=0&amp;repeatCount=1" />
    <to uri="http4:localhost:8084/example_json" />
    <to uri="bean:myBean" />
</route>

I got the following exception message: "Property ['key2'] not found in path
$" (stacktrace: http://pastebin.com/TMrK4wC0)

Everything works perfectly fine when I use only one @JsonPath method
argument, key1 or key2 it doesn't matter. But it doesn't work with two
arguments, regardless of their order. I always receive error message taht
the second parameter is not found in path "$" regardless which one is second
- key1 or key2.

The weird thing is it works perfectly fine with JSON read from filesystem
instead of http service:

<route id="test">
    <from uri="file:/tmp/example?noop=true" />
    <to uri="bean:myBean" />
</route>

Log messages indicate Camel reads the same file twice to evaluate two
JsonPath expressions, but I'm not sure it it's true:

2016.05.20 07:29:04.600 DEBUG Camel (camel-1) thread #0 -
file:///tmp/example org.apache.camel.component.file.GenericFileConverter -
Read file /tmp/example/example.json (no charset)
2016.05.20 07:29:04.607 DEBUG Camel (camel-1) thread #0 -
file:///tmp/example com.jayway.jsonpath.internal.CompiledPath - Evaluating
path: $['key1']
Got key1: val1
2016.05.20 07:29:57.518 DEBUG Camel (camel-1) thread #0 -
file:///tmp/example org.apache.camel.component.file.GenericFileConverter -
Read file /tmp/example/example.json (no charset)
2016.05.20 07:29:57.525 DEBUG Camel (camel-1) thread #0 -
file:///tmp/example com.jayway.jsonpath.internal.CompiledPath - Evaluating
path: $['key2']
Got key2: val2

I'm using Apache Camel 2.17.1 with Jayway Json Path 2.0.0.

BR,
Grzegorz



--
View this message in context: http://camel.465427.n5.nabble.com/Inject-multiple-JsonPath-parameters-to-bean-method-tp5782825.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Inject multiple @JsonPath parameters to bean method

Posted by grzechol <gr...@outlook.com>.
Converting body to String did the job. Thank you!



--
View this message in context: http://camel.465427.n5.nabble.com/Inject-multiple-JsonPath-parameters-to-bean-method-tp5782825p5782833.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Inject multiple @JsonPath parameters to bean method

Posted by Claus Ibsen <cl...@gmail.com>.
You can add a

covertBodyTo(string.class) before the bean so its not stream based.

It smells like the bean component need to reset the stream before
evaluation in case you have multiple parameters from the body which
you are doing, but which is otherwise rarely used.
https://issues.apache.org/jira/browse/CAMEL-9979

On Fri, May 20, 2016 at 1:45 PM, grzechol
<gr...@outlook.com> wrote:
> Hi Claus,
> Thank your for very quick response. Unfortunately, stream caching didn't
> help - it just changed the exception type.
>
> I've enabled stream caching on CamelContext level, and set spool threshold
> to 1 (just to see what will happen). The message body is cached and spooled
> to disk:
> 2016.05.20 13:36:25.924 DEBUG Camel (camel-1) thread #0 - timer://foo
> org.apache.camel.impl.DefaultStreamCachingStrategy - Should spool cache 29
> -> true
>
> But then I got:
>
> ExpressionEvaluationException: java.io.IOException: Stream closed (full
> stacktrace: http://pastebin.com/idtpACVU)
>
> when evaluating jsonpath for the first argument...
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Inject-multiple-JsonPath-parameters-to-bean-method-tp5782825p5782831.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: Inject multiple @JsonPath parameters to bean method

Posted by grzechol <gr...@outlook.com>.
Hi Claus,
Thank your for very quick response. Unfortunately, stream caching didn't
help - it just changed the exception type.

I've enabled stream caching on CamelContext level, and set spool threshold
to 1 (just to see what will happen). The message body is cached and spooled
to disk:
2016.05.20 13:36:25.924 DEBUG Camel (camel-1) thread #0 - timer://foo
org.apache.camel.impl.DefaultStreamCachingStrategy - Should spool cache 29
-> true

But then I got:

ExpressionEvaluationException: java.io.IOException: Stream closed (full
stacktrace: http://pastebin.com/idtpACVU)

when evaluating jsonpath for the first argument... 



--
View this message in context: http://camel.465427.n5.nabble.com/Inject-multiple-JsonPath-parameters-to-bean-method-tp5782825p5782831.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Inject multiple @JsonPath parameters to bean method

Posted by Claus Ibsen <cl...@gmail.com>.
Maybe you need stream caching
http://camel.apache.org/why-is-my-message-body-empty.html

On Fri, May 20, 2016 at 1:12 PM, grzechol
<gr...@outlook.com> wrote:
> Hi guys,
>
> I have a problem injecting multiple @JsonPath arguments using bean binding.
>
> Here is my sample json:
> {"key1":"val1","key2":"val2"}
>
> My bean method processing that JSON:
> @Handler
> public void process(@JsonPath("$.key1") String value1, @JsonPath("$.key2")
> String value2) {
>     System.out.println("Got key1: " + value1);
>     System.out.println("Got key2: " + value2);
> }
>
> And my route:
> <route id="test">
>     <from uri="timer://foo?period=0&amp;repeatCount=1" />
>     <to uri="http4:localhost:8084/example_json" />
>     <to uri="bean:myBean" />
> </route>
>
> I got the following exception message: "Property ['key2'] not found in path
> $" (stacktrace: http://pastebin.com/TMrK4wC0)
>
> Everything works perfectly fine when I use only one @JsonPath method
> argument, key1 or key2 it doesn't matter. But it doesn't work with two
> arguments, regardless of their order. I always receive error message taht
> the second parameter is not found in path "$" regardless which one is second
> - key1 or key2.
>
> The weird thing is it works perfectly fine with JSON read from filesystem
> instead of http service:
>
> <route id="test">
>     <from uri="file:/tmp/example?noop=true" />
>     <to uri="bean:myBean" />
> </route>
>
> Log messages indicate Camel reads the same file twice to evaluate two
> JsonPath expressions, but I'm not sure it it's true:
>
> 2016.05.20 07:29:04.600 DEBUG Camel (camel-1) thread #0 -
> file:///tmp/example org.apache.camel.component.file.GenericFileConverter -
> Read file /tmp/example/example.json (no charset)
> 2016.05.20 07:29:04.607 DEBUG Camel (camel-1) thread #0 -
> file:///tmp/example com.jayway.jsonpath.internal.CompiledPath - Evaluating
> path: $['key1']
> Got key1: val1
> 2016.05.20 07:29:57.518 DEBUG Camel (camel-1) thread #0 -
> file:///tmp/example org.apache.camel.component.file.GenericFileConverter -
> Read file /tmp/example/example.json (no charset)
> 2016.05.20 07:29:57.525 DEBUG Camel (camel-1) thread #0 -
> file:///tmp/example com.jayway.jsonpath.internal.CompiledPath - Evaluating
> path: $['key2']
> Got key2: val2
>
> I'm using Apache Camel 2.17.1 with Jayway Json Path 2.0.0.
>
> BR,
> Grzegorz
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Inject-multiple-JsonPath-parameters-to-bean-method-tp5782825.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2