You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by ychawla <pr...@yahoo.com> on 2012/08/14 20:27:02 UTC

JSON - Jackson return list rather than POJO

Hello All,
I am unmarshalling data using Jackson.  My dataFormats bean is set up as
such:

        <dataFormats>
            <json id="personSearchJacksonID" library="Jackson"
unmarshalTypeName="model.PersonSearchResponse"/>
        </dataFormats>

However, my return value isn't a simple POJO, it is a list of POJO's.  For
example this is a sample stripped down JSON response:

[{"last_name":"xxxxxxx",,"first_name":"Heather"},{"last_name":"xxxxxxx","first_name":"Leah"}]

It returns a list of PersonSearchResponses rather than just a single
PersonSearchResponse.

In a JUnit test, I did this:

List<PersonSearchResponse> personSearchResponses =
mapper.readValue(inputFile, new
TypeReference<List&lt;PersonSearchResponse>>() { });

Is there an equivalent that I can do in the Spring DSL?  Worse comes worse,
I write a custom bean and pass the JSON response as the body and manually do
'mapper.readValue' and then process the list.

I am on Camel 2.8.4 so I am not using camel-xmljson and converting my POJO
to XML manually.

Thanks,
Yogesh



--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JSON - Jackson return list rather than POJO

Posted by yagyesh <ya...@gmail.com>.
Can i get more insight the way this problem is solved ?
I have posted a similar question in camel forum
(http://camel.465427.n5.nabble.com/DataFormat-Versus-TypeConverters-td5747307.html#a5747369)



--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5747370.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: JSON - Jackson return list rather than POJO

Posted by "Daniel Gredler (DHL GM US)" <Da...@dhl.com>.
Ha!

Since the subject was JSON, I had assumed that the pun had to do with HTTP POST, not with forums... hence my follow up pun referencing HTTP GET :-)

Take care,

Daniel


-----Original Message-----
From: yourboogieman [mailto:eric_green86@hotmail.com] 
Sent: Saturday, July 19, 2014 3:19 PM
To: users@camel.apache.org
Subject: RE: JSON - Jackson return list rather than POJO

Well, a forum is a place you post things.  So, it's a bit of a play on words to say, "I'll keep yo posted."


-----Original Message-----
From: Daniel.Gredler@dhl.com
Sent: Thursday, July 17, 2014 2:29 PM
To: users@camel.apache.org
Subject: RE: JSON - Jackson return list rather than POJO

> I'll keep you all posted (pun intended) :)

I don't GET it.


-----Original Message-----
From: yourboogieman [mailto:eric_green86@hotmail.com] 
Sent: Wednesday, July 16, 2014 6:10 PM
To: users@camel.apache.org
Subject: Re: JSON - Jackson return list rather than POJO

I am being held up by this problem also, and I have an idea of how to fix it. 
I'm going to have a crack at CAMEL-5604.  I'll keep you all posted (pun
intended) :)

RE: JSON - Jackson return list rather than POJO

Posted by yourboogieman <er...@hotmail.com>.
Well, a forum is a place you post things.  So, it's a bit of a play on words
to say, "I'll keep yo posted."



--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5754036.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: JSON - Jackson return list rather than POJO

Posted by "Daniel Gredler (DHL GM US)" <Da...@dhl.com>.
> I'll keep you all posted (pun intended) :)

I don't GET it.


-----Original Message-----
From: yourboogieman [mailto:eric_green86@hotmail.com] 
Sent: Wednesday, July 16, 2014 6:10 PM
To: users@camel.apache.org
Subject: Re: JSON - Jackson return list rather than POJO

I am being held up by this problem also, and I have an idea of how to fix it. 
I'm going to have a crack at CAMEL-5604.  I'll keep you all posted (pun
intended) :)



--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5753927.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JSON - Jackson return list rather than POJO

Posted by yourboogieman <er...@hotmail.com>.
I am being held up by this problem also, and I have an idea of how to fix it. 
I'm going to have a crack at CAMEL-5604.  I'll keep you all posted (pun
intended) :)



--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5753927.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: JSON - Jackson return list rather than POJO

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Better late than newer. I have logged a ticket so we wont forget about this
https://issues.apache.org/jira/browse/CAMEL-5604


On Thu, Aug 23, 2012 at 9:01 AM, Claus Ibsen <cl...@gmail.com> wrote:
> On Mon, Aug 20, 2012 at 10:42 AM, Jason Chaffee
> <Ja...@betfair.com> wrote:
>> I asked this same question before and this the example and answer I got about using a Map with Jackson.
>>
>
> Yeah I think it uses Map by default.
>
>>
>> <dataFormats>
>>      <!-- here we define a Json data format with the id jack and that it should use the TestPojo as the class type when
>>             doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type -->
>>      <json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"/>
>> </dataFormats>
>>
>> I did not test it to confirm that it works correctly though.
>>
>> On Aug 20, 2012, at 1:08 AM, Claus Ibsen <cl...@gmail.com>> wrote:
>>
>> Hi
>>
>> Maybe we could improve the camel-jackson to support list/map types as
>> responses more easily?
>> I just wonder what a good solution would be?
>>
>> Either the user would need to explicit configure this on the data format?
>>
>> Or we can look into a fallback type converter, so Camel automatic an
>> convert to a List/Map for you.
>> And if you want you can use
>>
>> <convertBodyTo type="java.util.List"/>
>>
>> And then the fallback type converter would kick in.
>>
>>
>>
>> On Tue, Aug 14, 2012 at 9:43 PM, ychawla <pr...@yahoo.com>> wrote:
>> A simple workaround for this is a custom bean processor like so:
>>
>>        public void processPersonSearchResponseJSON(Exchange exchange) throws
>> JsonParseException, JsonMappingException, IOException
>>        {
>>                ObjectMapper mapper = new ObjectMapper();
>>
>>                List<PersonSearchResponse> personSearchResponses =
>> mapper.readValue(exchange.getIn().getBody(String.class), new
>> TypeReference<List<PersonSearchResponse>>() { });
>>
>>                exchange.getIn().setBody(personSearchResponses);
>>
>>        }
>>
>> and in the camel context:
>>
>> <camel:to uri="JSONEndpoint"/>
>>
>> <camel:convertBodyTo type="java.lang.String"/>
>>
>> <camel:log message="This is the JSON Response: ${body}" />
>>
>> <camel:to
>> uri="bean:personSearchResponseJSONProcessor?method=processPersonSearchResponseJSON"/>
>>
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5717347.html
>> Sent from the Camel - Users mailing list archive at Nabble.com<http://Nabble.com>.
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> FuseSource
>> Email: cibsen@fusesource.com<ma...@fusesource.com>
>> Web: http://fusesource.com
>> Twitter: davsclaus, fusenews
>> Blog: http://davsclaus.com
>> Author of Camel in Action: http://www.manning.com/ibsen
>>
>>
>> ________________________________________________________________________
>> In order to protect our email recipients, Betfair Group use SkyScan from
>> MessageLabs to scan all Incoming and Outgoing mail for viruses.
>>
>> ________________________________________________________________________
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: JSON - Jackson return list rather than POJO

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Aug 20, 2012 at 10:42 AM, Jason Chaffee
<Ja...@betfair.com> wrote:
> I asked this same question before and this the example and answer I got about using a Map with Jackson.
>

Yeah I think it uses Map by default.

>
> <dataFormats>
>      <!-- here we define a Json data format with the id jack and that it should use the TestPojo as the class type when
>             doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type -->
>      <json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"/>
> </dataFormats>
>
> I did not test it to confirm that it works correctly though.
>
> On Aug 20, 2012, at 1:08 AM, Claus Ibsen <cl...@gmail.com>> wrote:
>
> Hi
>
> Maybe we could improve the camel-jackson to support list/map types as
> responses more easily?
> I just wonder what a good solution would be?
>
> Either the user would need to explicit configure this on the data format?
>
> Or we can look into a fallback type converter, so Camel automatic an
> convert to a List/Map for you.
> And if you want you can use
>
> <convertBodyTo type="java.util.List"/>
>
> And then the fallback type converter would kick in.
>
>
>
> On Tue, Aug 14, 2012 at 9:43 PM, ychawla <pr...@yahoo.com>> wrote:
> A simple workaround for this is a custom bean processor like so:
>
>        public void processPersonSearchResponseJSON(Exchange exchange) throws
> JsonParseException, JsonMappingException, IOException
>        {
>                ObjectMapper mapper = new ObjectMapper();
>
>                List<PersonSearchResponse> personSearchResponses =
> mapper.readValue(exchange.getIn().getBody(String.class), new
> TypeReference<List<PersonSearchResponse>>() { });
>
>                exchange.getIn().setBody(personSearchResponses);
>
>        }
>
> and in the camel context:
>
> <camel:to uri="JSONEndpoint"/>
>
> <camel:convertBodyTo type="java.lang.String"/>
>
> <camel:log message="This is the JSON Response: ${body}" />
>
> <camel:to
> uri="bean:personSearchResponseJSONProcessor?method=processPersonSearchResponseJSON"/>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5717347.html
> Sent from the Camel - Users mailing list archive at Nabble.com<http://Nabble.com>.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com<ma...@fusesource.com>
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>
>
> ________________________________________________________________________
> In order to protect our email recipients, Betfair Group use SkyScan from
> MessageLabs to scan all Incoming and Outgoing mail for viruses.
>
> ________________________________________________________________________



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: JSON - Jackson return list rather than POJO

Posted by Jason Chaffee <Ja...@betfair.com>.
I asked this same question before and this the example and answer I got about using a Map with Jackson.


<dataFormats>
     <!-- here we define a Json data format with the id jack and that it should use the TestPojo as the class type when
            doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type -->
     <json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"/>
</dataFormats>

I did not test it to confirm that it works correctly though.

On Aug 20, 2012, at 1:08 AM, Claus Ibsen <cl...@gmail.com>> wrote:

Hi

Maybe we could improve the camel-jackson to support list/map types as
responses more easily?
I just wonder what a good solution would be?

Either the user would need to explicit configure this on the data format?

Or we can look into a fallback type converter, so Camel automatic an
convert to a List/Map for you.
And if you want you can use

<convertBodyTo type="java.util.List"/>

And then the fallback type converter would kick in.



On Tue, Aug 14, 2012 at 9:43 PM, ychawla <pr...@yahoo.com>> wrote:
A simple workaround for this is a custom bean processor like so:

       public void processPersonSearchResponseJSON(Exchange exchange) throws
JsonParseException, JsonMappingException, IOException
       {
               ObjectMapper mapper = new ObjectMapper();

               List<PersonSearchResponse> personSearchResponses =
mapper.readValue(exchange.getIn().getBody(String.class), new
TypeReference<List<PersonSearchResponse>>() { });

               exchange.getIn().setBody(personSearchResponses);

       }

and in the camel context:

<camel:to uri="JSONEndpoint"/>

<camel:convertBodyTo type="java.lang.String"/>

<camel:log message="This is the JSON Response: ${body}" />

<camel:to
uri="bean:personSearchResponseJSONProcessor?method=processPersonSearchResponseJSON"/>




--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5717347.html
Sent from the Camel - Users mailing list archive at Nabble.com<http://Nabble.com>.



--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com<ma...@fusesource.com>
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen


________________________________________________________________________
In order to protect our email recipients, Betfair Group use SkyScan from 
MessageLabs to scan all Incoming and Outgoing mail for viruses.

________________________________________________________________________

Re: JSON - Jackson return list rather than POJO

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Maybe we could improve the camel-jackson to support list/map types as
responses more easily?
I just wonder what a good solution would be?

Either the user would need to explicit configure this on the data format?

Or we can look into a fallback type converter, so Camel automatic an
convert to a List/Map for you.
And if you want you can use

<convertBodyTo type="java.util.List"/>

And then the fallback type converter would kick in.



On Tue, Aug 14, 2012 at 9:43 PM, ychawla <pr...@yahoo.com> wrote:
> A simple workaround for this is a custom bean processor like so:
>
>         public void processPersonSearchResponseJSON(Exchange exchange) throws
> JsonParseException, JsonMappingException, IOException
>         {
>                 ObjectMapper mapper = new ObjectMapper();
>
>                 List<PersonSearchResponse> personSearchResponses =
> mapper.readValue(exchange.getIn().getBody(String.class), new
> TypeReference<List<PersonSearchResponse>>() { });
>
>                 exchange.getIn().setBody(personSearchResponses);
>
>         }
>
> and in the camel context:
>
> <camel:to uri="JSONEndpoint"/>
>
> <camel:convertBodyTo type="java.lang.String"/>
>
> <camel:log message="This is the JSON Response: ${body}" />
>
> <camel:to
> uri="bean:personSearchResponseJSONProcessor?method=processPersonSearchResponseJSON"/>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5717347.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: JSON - Jackson return list rather than POJO

Posted by ychawla <pr...@yahoo.com>.
A simple workaround for this is a custom bean processor like so:

	public void processPersonSearchResponseJSON(Exchange exchange) throws
JsonParseException, JsonMappingException, IOException
	{
		ObjectMapper mapper = new ObjectMapper();
		 
		List<PersonSearchResponse> personSearchResponses =
mapper.readValue(exchange.getIn().getBody(String.class), new
TypeReference<List&lt;PersonSearchResponse>>() { });
		  
		exchange.getIn().setBody(personSearchResponses);

	}

and in the camel context:

<camel:to uri="JSONEndpoint"/>
			
<camel:convertBodyTo type="java.lang.String"/>
			
<camel:log message="This is the JSON Response: ${body}" />
			
<camel:to
uri="bean:personSearchResponseJSONProcessor?method=processPersonSearchResponseJSON"/>




--
View this message in context: http://camel.465427.n5.nabble.com/JSON-Jackson-return-list-rather-than-POJO-tp5717341p5717347.html
Sent from the Camel - Users mailing list archive at Nabble.com.