简体   繁体   中英

Best approach to modify body in Apache Camel?

I was wondering what is the best solution for doing simple transformation to the body of the message in a given route. The body is going to be CSV and I want to modify some of the fields if some conditions are met. I want to know if there is any built-in functionality that I can use before start writing my own processor.

I have been taking a look to the scripting languages supported by Camel but I have not been able to find a good example. I believe this can be done with Expressions using Groovy or other of the scripting languages, but I need some guidance.

Could anybody help me out?

Thanks!

There are actually two separate things. Accessing (unmarshal/marshal) the CSV data in a structured way and then do the actual logic to transform the data.

You can use the CSV data format (or flatpack ) to get your CSV data into a List of List which you easily manipulate in a plain Java bean (or groovy if you want). Then you can take your result and turn it into CSV again by the same component.

If you have large CSV structures and very complex transformations - you might want to map your CSV file to a list of Objects instead of a List of List of String. You can define classes for the input and output data, then bind those classes to CSV rows using the Bindy component. Then, in the middle you will only have to deal with mapping the fields of java objects, which is rather straight forward. If you want to dig down into frameworks for mapping one java object to another, you could look at Dozer which is quite good (but overkill in most cases).

As you state, you could use groovy or similar for the actual mapping. It should be rather straight forward, even though I am not aware of much examples on the topic.

<transform>
  <groovy> response.f1 = request.f2  </groovy>
</transform>

where you have the objects request and response available among others.

Yet another way to do it is to use the larger framework Smooks . It handles parsing and mapping between many different data formats. It's not part of Camel, but Smooks has Camel support built in and is open source. Might be overkill if you do just a few smaller mappings, but might be worth it if you deal with complex mapping scenarios over and over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM