Apache Camel Cannot modify json response

973 Views Asked by At

I'm bothered with returning an updated JSON, in fact, I receive a JSON defined as an opportunity (it's a business term, we don't need to explain it) from the cxfrs endpoint, convert it to String, then check the method invoked in jax-rs controller, if the we want make an update of Affair json, we publish the message as string in queue q.gestioncougar.cl.creation as described below:

<route id="serviceToCustomerLinksInboundRouting">
    <from uri="cxfrs:bean:oab_serviceToObsIT?loggingFeatureEnabled={{gestioncougar.wscall.log.enable}}&amp;loggingSizeLimit={{gestioncougar.wscall.log.size}}" />
        <to uri="bean:log?method=info(*,'Body : ${body}')"/>
        <convertBodyTo type="java.lang.String" />            
        <choice>
            <when>
                <simple>${header.operationName} == 'updateAffair'</simple>
                    <to uri="activemq:queue:q.gestioncougar.cl.creation?disableReplyTo=true" />
            </when>
        </choice>
</route>

The config of cxfrs service that is bonded to jax-rs controller:

<cxf:rsServer address="{{gestioncougar.service.in.obsit.url}}" id="oab_serviceToObsIT"
    serviceClass="fr.oab.sie.esb.gestioncougar.customerLink.controller.CougarToCL" />

The JAX-RS controller:

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface CougarToCL {

    @PUT
    @Path("/updateAffair")
    void updateAffair(String request);

}

Then we create a route that read from the queue q.gestioncougar.cl.creation, next unmarshal the body to POJO format (Java Model), moreover, read it by the method convertToCLFormat defined in the bean traiterCallFromCougar:

<route id="serviceToCustomerLinksCreationOutboundRoute">
    <from uri="activemq:queue:q.gestioncougar.cl.creation" />
        <!-- Sauvegarde du body initial -->
        <unmarshal ref="formatJsonOpportunity" />
        <!-- Sauvegarde du body -->
        <setProperty propertyName="savedBody">
            <simple>${body}</simple>
        </setProperty>
        <bean ref="traiterCallFromCougar" method="convertToCLFormat"/>
        <to uri="bean:log?method=info(*,'CustomerLinks Body Format: ${body}')"/>
</route>

The method convertToCLFormat:

public void convertToCLFormat(Exchange exchange){
    Opportunity opportunity = exchange.getProperty("savedBody",Opportunity.class);
    EsbLogger.info(exchange, "opportunity {}", opportunity.toString());
    exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
    Affair affair = opportunityAffairMapper.toDealCL(opportunity.getDealCL());
    String clBody = affairJsonParser.parsePojoToJson(affair);
    EsbLogger.info(exchange, "affair {}", clBody);
    exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    exchange.getOut().setBody(clBody);
}

and here is the problem !, even if I set the body with the newly mapped JSON defined as Affair, I don't see any change in postman response, but the body is changed when I check the log.

Take a look at the postman request:

enter image description here

Thanks a lot for your time and your help.

1

There are 1 best solutions below

1
On

Do not put the converted JSON into the "out" message, but rather in the "in" one.

This was so confusing that they now recommend to simply use exchange.getMessage() (without any distinction whether in or out)

See more here: https://camel.apache.org/manual/latest/camel-3-migration-guide.html#_getout_on_exchange

So this should work:

exchange.getMessage().setBody(clBody);