@Bean @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(JacksonDataFormat.class) public JacksonDataFormat configureJacksonDataFormat( CamelContext camelContext, JacksonDataFormatConfiguration configuration) throws Exception { JacksonDataFormat dataformat = new JacksonDataFormat(); if (dataformat instanceof CamelContextAware) { ((CamelContextAware) dataformat).setCamelContext(camelContext); } Map<String, Object> parameters = new HashMap<>(); IntrospectionSupport.getProperties(configuration, parameters, null, false); IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters); return dataformat; }
@Before public void init() { ObjectMapper mapper = ObjectMapperFactory.createInstance(); dataFormat = new JacksonDataFormat(mapper, Object.class); String expectedClassMapKey1 = isClassMapKeySerializedWithPrefix() ? "class java.lang.Long" : "java.lang.Long"; expectedJson = "{" + "\"@class\":\"" + SimplePojo.class.getCanonicalName() + "\"," + "\"field1\":\"" + EXPECTED_FIELD1_VALUE + "\"," + "\"field2\":" + EXPECTED_FIELD2_VALUE + "," + "\"field3\":\"" + EXPECTED_FIELD3_VALUE + "\"," + "\"field4\":{\"@class\":\"java.util.HashMap\"," + "\"" + expectedClassMapKey1 + "\":\"" + EXPECTED_CLASS_MAP_VALUE1 + "\"}," + "\"field5\":{\"@class\":\"java.util.HashMap\"," + "\"" + EXPECTED_STRING_MAP_KEY1 + "\":\"" + EXPECTED_STRING_MAP_VALUE1 + "\"}" + "}"; }
@Override public void configure() throws Exception { String s = null; // Create an instance of JacksonDataFormat to convert to JSON to // a {@link Notification} instance JacksonDataFormat format = new JacksonDataFormat(); format.setUnmarshalType(Notification.class); from(getFromUri()) .routeId(getRouteId()) .startupOrder(getStartUpOrder()) // .log(DEBUG,"headers: ${headers}") // .log(DEBUG,"body: ${body}") // .log(DEBUG,"body: ${body}") // .log(DEBUG,"class: ${body.getClass.toString}") // .process(new Processor() { // public void process(Exchange exchange) throws Exception { // HttpMessage msg = exchange.getIn(HttpMessage.class); // // InputStreamCache sis = msg.getBody(InputStreamCache.class); // // String s = exchange.getContext().getTypeConverter().convertTo(String.class, sis); // LOG.debug("process body: " + s); // // Message newMessage = new DefaultMessage(); // newMessage.setHeaders(msg.getHeaders()); // newMessage.setBody(s); // exchange.setIn(newMessage); // } // }) // .log(DEBUG,"BEFORE CLASS: ${body.getClass.toString}") // .log(DEBUG,"BEFORE SIZE: ${body.length}") // .log(DEBUG,"BEFORE BODY: ${body}") // .unmarshal().string("UTF-8") // .log(DEBUG,"AFTER CLASS: ${body.getClass.toString}") // .log(DEBUG,"AFTER SIZE: ${body.length}") // .log(DEBUG,"AFTER BODY: ${body}") .unmarshal().json(JsonLibrary.Jackson,Notification.class) .to(getToUri()); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() { JacksonDataFormat jdf = new JacksonDataFormat(TemperaturePojo.class); from("direct:start") .unmarshal(jdf) .to("mock:result"); } }; }
/** * Initializes a Camel context and configures routes and object marshaling with the given * brokerUrl, enpoint, and messageConsumer. * * @param brokerUrl * @param endpoint * @param messageConsumer * @return the Gytheio message producer * @throws Exception */ protected MessageProducer initializeCamelEndpoint( final String brokerUrl, final String endpointSend, final String endpointReceive, final MessageConsumer messageConsumer) throws Exception { CamelContext context = new DefaultCamelContext(); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl); JmsComponent component = AMQPComponent.jmsComponent(); component.setConnectionFactory(connectionFactory); context.addComponent("amqp", component); final DataFormat dataFormat = new JacksonDataFormat( ObjectMapperFactory.createInstance(), Object.class); if (messageConsumer != null) { context.addRoutes(new RouteBuilder() { public void configure() { from("amqp:" + endpointReceive).unmarshal(dataFormat).bean(messageConsumer, "onReceive"); } }); } context.addRoutes(new RouteBuilder() { public void configure() { from("direct:benchmark.test").marshal(dataFormat).to("amqp:" + endpointSend); } }); CamelMessageProducer messageProducer = new CamelMessageProducer(); messageProducer.setProducer(context.createProducerTemplate()); messageProducer.setEndpoint("direct:benchmark.test"); context.start(); return messageProducer; }
@Test public void testJsonIgnore() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(); jacksonDataFormat.setPrettyPrint(false); from("direct:start") .process(new Processor() { @Override public void process(Exchange exchange) throws Exception { Organization organization = new Organization(); organization.setName("The Organization"); Employee employee = new Employee(); employee.setName("The Manager"); employee.setEmployeeNumber(12345); employee.setOrganization(organization); organization.setManager(employee); exchange.getIn().setBody(employee); } }) .marshal(jacksonDataFormat); } }); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); String result = template.requestBody("direct:start", null, String.class); Assert.assertEquals("{\"name\":\"The Manager\"}", result); } finally { camelctx.stop(); } }
@Test public void testRestSwaggerJSON() throws Exception { JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(); jacksonDataFormat.setUnmarshalType(Customer.class); CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:getCustomerById") .to("customer:getCustomerById") .convertBodyTo(String.class) .unmarshal(jacksonDataFormat); } }); RestSwaggerComponent restSwaggerComponent = new RestSwaggerComponent(); restSwaggerComponent.setSpecificationUri(new URI("http://localhost:8080/api/swagger")); restSwaggerComponent.setComponentName("undertow"); restSwaggerComponent.setConsumes(MediaType.APPLICATION_JSON); restSwaggerComponent.setProduces(MediaType.APPLICATION_JSON); camelctx.addComponent("customer", restSwaggerComponent); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); Customer customer = template.requestBodyAndHeader("direct:getCustomerById", null, "id", 1, Customer.class); Assert.assertNotNull(customer); Assert.assertEquals(1, customer.getId()); } finally { camelctx.stop(); } }
@Override public void configure() throws Exception { DataFormat jsonDataFormat = new JacksonDataFormat(); from("cxfrs:bean:rsReportsServer?bindingStyle=SimpleConsumer") .log("Executing ${header.operationName}") .doTry() .beanRef("authBean", "isLoggedIn") .beanRef("authBean", "canAccessReport") .recipientList(simple("direct:rs-${header.operationName}")).end() .doCatch(AuthenticationException.class) .log("Authentication failed ${header.operationName}") .beanRef("authBean", "buildAuthFail") .marshal(jsonDataFormat) .doCatch(AuthorizationException.class) .log("Report authorization failed ${header.operationName} ${header.reportId}") .beanRef("reportBean", "noSuchReport") .marshal(jsonDataFormat) .end() .routeId("cxfrsReportsInRouteId"); from("direct:rs-reportDetails") .beanRef("reportBean", "getReportsDetailed") .marshal().json(JsonLibrary.Jackson) .routeId("rsReportDetailsRouteId"); from("direct:rs-runReport") .beanRef("reportBean", "run") .marshal().json(JsonLibrary.Jackson) .routeId("rsRunReportRouteId"); from("direct:rs-detailsAndRunReport") .beanRef("reportBean", "detailsAndRun") .marshal().json(JsonLibrary.Jackson) .routeId("rsDetailsAndRunReportRouteId"); from("direct:rs-exportReport") .beanRef("reportBean", "export") .routeId("rsExportReportRouteId"); }
@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry jndi = super.createRegistry(); jndi.bind("bla", new JacksonDataFormat()); return jndi; }