First, I'm fairly new to Camel so if what (or how) I'm trying to do here is dumb, let me know.

CODE:

from("direct:one")
        .to("mock:two")
        .process(new Processor(){ 
                @Override
                public void process(Exchange exchange)throws Exception{
                        MyCustomObject obj = exchange.getIn().getBody(MyCustomObject.class);
                    exchange.getOut().setBody(obj.getOneOfTheFields());
                }
        })
        .to("mock:three");

QUESTION:

This processor transforms an object to one of it's fields. I know that I could replace it with simple expression but that would require me to put 'oneOfTheFields' in a string and I don't want to do that.

Is there a shorter way to do this using java code only?

2 Answers

1
Ramin Arabbagheri On

Can you not simply do this:

from("direct:one")
    .to("mock:two")
    .setBody(body().getOneOfTheFields())
    .to("mock:three");

Let me know if this works.

1
icabod On

This can be easily achieved using setBody and Camel simple:

from("direct:one")
  .to("mock:two")
  .setBody(simple("${body.fieldName}"))
  .to("mock:three");

You specify the name of the field, and Camel will use the standard accessor mechanism to set the body appropriately.