0

I am using Apache Camel 2.14.0 to build a REST API, and am using the Rest DSL and its auto binding to POJOs, I believe this uses Jackson for (De)Serialization. Unfortunately this produces rather long and unweildy JSON from DateTime objects including fields like chronology, zone, uncachedZone, etc.

I would like it to create/consume something simpler.

I am aware from this question: How to serialize Joda DateTime with Jackson JSON processer?

That I can register the JodaModule for a Jackson ObjectMapper like so:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

But I cannot work out or find an example of how to achieve this in Camel.

How would I go about doing this? Something in my camel context, or something completely different? I would like to avoid writing a custom (de)seralizer and having to annotate each DateTime field separately.

I do not need a custom implementation, just the simple one provided by the JodaModule.

Alternatively, if Jackson serializes a different Date/Time object better by default, I could use that.

Community
  • 1
  • 1
  • 1
    Yeah I think we should make this easy out of the box, so I logged a ticket: https://issues.apache.org/jira/browse/CAMEL-8176 – Claus Ibsen Dec 24 '14 at 08:48
  • @ClausIbsen Is this currently not possible then? Necessary to annotate each field invidually and use a custom deserializer? Thanks for logging the ticket –  Dec 24 '14 at 11:09
  • I haven't looked how hard/easy it is today - just made sure to record a ticket about this – Claus Ibsen Dec 24 '14 at 12:30

1 Answers1

0

First of all, from what I remember marshal and unmashal may take as a parameter the library you want to do the job like:

.marshal().json(JsonLibrary.Jackson, MyObject.class)

I think default is Xstream but I can't be sure.

Anyway, I believe an annotation on POJO like

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm a z")

should do the trick bu as you said you want to avoid annotating every single pojo.

Alternatively you may create a Processor that will run after the JSON creation.

The last option is to write a custom JsonDeserializer (http://jackson.codehaus.org/1.5.3/javadoc/org/codehaus/jackson/map/JsonDeserializer.html)

mdagis
  • 116
  • 1
  • 1
  • 6