Mar
Mar

Reputation: 443

How to make Camel route Exchange to retrieve original data type?

Im executing Camel route which receives body/parameter from FluentProducerTemplate using withBody(parameter) and then in a r oute I want to execute carService(CarReg carReg) method into which I want to pass parameter which I have sent from producer.

Route is working, when I am testing it with executing method which doesnt required parameter and then called method and route return requested data. Also the body/parameter Im sending is correctly passed into route exchange since in debug mode I can see that exchange holds correct data I have passed.

The issue is when I want in an exchange to call method into which I want to pass parameter which is supposed to be retrieved by the exchange, then this code is flagged as an error getCarDetails(exchange.getIn().getBody)) stating that Im passing just Object and not required CarReg object type. How to ensure that parameter retrieved by the exchange is of the original type and not an Object type?

 @EndpointInject
 private FluentProducerTemplate fProducerTemplate

  public CarDetails startRoute(CarReg carReg){
    CarDetails carDetails = fProducerTemplate.withBody(carReg)
       .to("direct:route") 
       .request(CarDetails .class)  
  }

@Override
public void configure() throw Exception
   from("direct:route")
      .process(exchange -> {
         exchange.getIn().getBody();    //exchange correctly retrieves passed parameter              
        
         exchange.get().setBody(carService.getCarDetails(exchange.getIn().getBody));
       });
}

Upvotes: 0

Views: 1738

Answers (3)

madhairsilence
madhairsilence

Reputation: 3880

You can type case it using Jackson library as well

@Autowired
ObjectMapper objectMapper;

String body = exchange.getIn().getBody();

CarReg carReg = objectMapper.convertValue( body , CarReg.class );

Upvotes: 0

Strelok
Strelok

Reputation: 51481

Message.getBody() has an overload that takes a class as parameter. So:

exchange.getOut().setBody(carService.getCarDetails(exchange.getIn().getBody(CarReg.class)));

Upvotes: 1

Raushan Kumar
Raushan Kumar

Reputation: 1248

Please try this, I hope this should work.

       @Override
    public void configure() throw Exception
    from("direct:route")
      .process(exchange -> {
        exchange.getIn().getBody();    //exchange correctly retrieves passed parameter

        exchange.get().setBody(carService.getCarDetails(exchange.getIn().getBody((CarReg.class))));
    });
}

Upvotes: 1

Related Questions