Skip to content

Instantly share code, notes, and snippets.

@bedlaj
Created August 9, 2019 21:38
Show Gist options
  • Save bedlaj/aaa5c80ed8cc4c64308e7fbd1d7d57f1 to your computer and use it in GitHub Desktop.
Save bedlaj/aaa5c80ed8cc4c64308e7fbd1d7d57f1 to your computer and use it in GitHub Desktop.
Camel JSON Content Enricher
package eu.janbednar.stackoverflow;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Assert;
import org.junit.Test;
import java.util.Map;
public class EnrichJson extends CamelTestSupport {
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
DataFormat json = new JacksonDataFormat();
from("direct:json")
.unmarshal(json)
.enrich("direct:callService", (original, response) -> {
original.getIn().getBody(Map.class).put(
"text_1",
response.getIn().getBody(String.class)
);
return original;
})
.marshal(json)
.to("log:result");
from("direct:callService")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setBody(simple("${body[text_1]}"))
.to("http4://httpbin.org/post");
}
};
}
@Test
public void test() throws Exception {
String result = template.requestBody("direct:json",
"{" +
" \"text_1\": \"some text input\"," +
" \"text_2\": \"some other text input\"" +
"}",
String.class
);
Assert.assertTrue(result.contains("httpbin.org"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment