org.apache.camel.model.dataformat.JsonLibrary Java Examples
The following examples show how to use
org.apache.camel.model.dataformat.JsonLibrary.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testUnmarshalJohnzon() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal().json(JsonLibrary.Johnzon, Customer.class); } }); String input = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = producer.requestBody("direct:start", input.replace('\'', '"'), Customer.class); Assert.assertEquals("John", customer.getFirstName()); Assert.assertEquals("Doe", customer.getLastName()); } finally { camelctx.close(); } }
Example #2
Source File: XstreamRoutes.java From camel-quarkus with Apache License 2.0 | 6 votes |
@Override public void configure() { from("direct:xstream-json-marshal") .marshal() .json(JsonLibrary.XStream); from("direct:xstream-json-unmarshal") .unmarshal() .json(JsonLibrary.XStream, PojoA.class); from("direct:xstream-xml-marshal") .marshal() .xstream("UTF-8"); from("direct:xstream-xml-unmarshal") .unmarshal() .xstream(PojoA.class); }
Example #3
Source File: Routes.java From camel-quarkus with Apache License 2.0 | 6 votes |
@Override public void configure() throws Exception { from("platform-http:/fruits?httpMethodRestrict=GET,POST") .choice() .when(simple("${header.CamelHttpMethod} == 'GET'")) .setBody() .constant(fruits) .endChoice() .when(simple("${header.CamelHttpMethod} == 'POST'")) .unmarshal() .json(JsonLibrary.Jackson, Fruit.class) .process() .body(Fruit.class, fruits::add) .setBody() .constant(fruits) .endChoice() .end() .marshal().json(); from("platform-http:/legumes?httpMethodRestrict=GET") .setBody().constant(legumes) .marshal().json(); }
Example #4
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testMarshalJohnzon() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal().json(JsonLibrary.Johnzon); } }); String expected = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class); Assert.assertEquals(expected.replace('\'', '"'), result); } finally { camelctx.close(); } }
Example #5
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testUnmarshalGson() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal().json(JsonLibrary.Gson, Customer.class); } }); String input = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = producer.requestBody("direct:start", input.replace('\'', '"'), Customer.class); Assert.assertEquals("John", customer.getFirstName()); Assert.assertEquals("Doe", customer.getLastName()); } finally { camelctx.close(); } }
Example #6
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testMarshalGson() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal().json(JsonLibrary.Gson); } }); String expected = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class); Assert.assertEquals(expected.replace('\'', '"'), result); } finally { camelctx.close(); } }
Example #7
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testUnmarshalJackson() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal().json(JsonLibrary.Jackson, Customer.class); } }); String input = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = producer.requestBody("direct:start", input.replace('\'', '"'), Customer.class); Assert.assertEquals("John", customer.getFirstName()); Assert.assertEquals("Doe", customer.getLastName()); } finally { camelctx.close(); } }
Example #8
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testMarshalJackson() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal().json(JsonLibrary.Jackson); } }); String expected = "{'firstName':'John','lastName':'Doe'}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class); Assert.assertEquals(expected.replace('\'', '"'), result); } finally { camelctx.close(); } }
Example #9
Source File: AgentKT.java From funktion-connectors with Apache License 2.0 | 6 votes |
private void assertWaitForResults(String namespace, String elasticSearchResource, SamplePayload payload) throws Throwable { final String pollUrl = pathJoin(elasticSearchResource, "/_source"); LOG.info("Querying elasticsearch URL: " + pollUrl); CamelTester.assertIsSatisfied(new TestRouteBuilder() { @Override protected void configureTest() throws Exception { // expectations results.expectedBodiesReceived(payload); results.setResultWaitTime(60 * 1000L); from("timer://poller?delay=10000&period=2000").errorHandler(deadLetterChannel(errors)). to(pollUrl). unmarshal().json(JsonLibrary.Jackson, SamplePayload.class). to(results); } }); }
Example #10
Source File: BookRouteConfiguration.java From spring-cloud-contract with Apache License 2.0 | 6 votes |
@Bean RoutesBuilder myRouter(final BookService bookService, final BookDeleter bookDeleter) { return new RouteBuilder() { @Override public void configure() throws Exception { // scenario 1 - from bean to output from("direct:start").unmarshal() .json(JsonLibrary.Jackson, BookReturned.class).bean(bookService) .to("jms:output"); // scenario 2 - from input to output from("jms:input").unmarshal() .json(JsonLibrary.Jackson, BookReturned.class).bean(bookService) .to("jms:output"); // scenario 3 - from input to no output from("jms:delete").unmarshal() .json(JsonLibrary.Jackson, BookDeleted.class).bean(bookDeleter); } }; }
Example #11
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testUnmarshalXStream() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal().json(JsonLibrary.XStream, Customer.class); } }); String input = "{'" + Customer.class.getName() + "':{'firstName':'John','lastName':'Doe'}}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer customer = producer.requestBody("direct:start", input, Customer.class); Assert.assertEquals("John", customer.getFirstName()); Assert.assertEquals("Doe", customer.getLastName()); } finally { camelctx.close(); } }
Example #12
Source File: JSONDataFormatTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
@Test public void testMarshalXStream() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal().json(JsonLibrary.XStream); } }); String expected = "{'" + Customer.class.getName() + "':{'firstName':'John','lastName':'Doe'}}"; camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class); Assert.assertEquals(expected.replace('\'', '"'), result); } finally { camelctx.close(); } }
Example #13
Source File: CamelExampleMain.java From blog with MIT License | 6 votes |
@Override public void configure() { from("timer://trigger"). // We are using camel http4 component to("http4://api.openweathermap.org/data/2.5/weather?q=London,uk"). // We need to convert the data stream to a string for the JSON marshaler convertBodyTo(String.class). unmarshal().json(JsonLibrary.Jackson, WeatherInformation.class). process(exchange -> { // Due to the unmarshal command a WeatherInformation is now set on the body WeatherInformation weatherInformation = exchange.getIn().getBody(WeatherInformation.class); // We just need the name of the city and the wind speed exchange.getOut().setBody(weatherInformation.getName() + ", " + weatherInformation.getWind().getSpeed() + System.lineSeparator()); }). to("file://?fileName=test.csv&fileExist=Append"). // Just for Logging to("log:out", "mock:test"); }
Example #14
Source File: PurchaseOrderJSONTest.java From camelinaction2 with Apache License 2.0 | 5 votes |
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("jetty://http://0.0.0.0:8080/order/service") .bean("orderService", "lookup") .marshal().json(JsonLibrary.Jackson); } }; }
Example #15
Source File: JsonJacksonRoute.java From camel-cookbook-examples with Apache License 2.0 | 5 votes |
@Override public void configure() throws Exception { from("direct:marshal") .marshal().json(JsonLibrary.Jackson) .to("mock:marshalResult"); from("direct:unmarshal") .unmarshal().json(JsonLibrary.Jackson, View.class) .to("mock:unmarshalResult"); }
Example #16
Source File: JsonRoute.java From camel-cookbook-examples with Apache License 2.0 | 5 votes |
@Override public void configure() throws Exception { from("direct:marshal") .marshal().json(JsonLibrary.XStream) .to("mock:marshalResult"); from("direct:unmarshal") .unmarshal().json(JsonLibrary.XStream, View.class) .to("mock:unmarshalResult"); }
Example #17
Source File: PurchaseOrderJSONTest.java From camelinaction with Apache License 2.0 | 5 votes |
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("jetty://http://0.0.0.0:8080/order/service") .beanRef("orderService", "lookup") .marshal().json(JsonLibrary.Jackson); } }; }
Example #18
Source File: ExecuteCommandsFromJmsRoute.java From hacep with Apache License 2.0 | 5 votes |
@Override public void configure() throws Exception { from("jms:" + queueName) .setExchangePattern(ExchangePattern.InOut) .to("log:it.redhat.hacep.camel.Router?level=INFO&showAll=true&multiline=true") .unmarshal().json(JsonLibrary.Jackson, Command.class) .to("direct:execute-command"); from("direct:execute-command") .setExchangePattern(ExchangePattern.InOut) .recipientList() .simple("direct:${body.command}"); }
Example #19
Source File: JsonMarshalAction.java From syndesis-extensions with Apache License 2.0 | 5 votes |
@Override public Optional<ProcessorDefinition<?>> configure(CamelContext context, ProcessorDefinition<?> route, Map<String, Object> parameters) { ObjectHelper.notNull(route, "route"); ObjectHelper.notNull(kind, "kind"); return Optional.of(route.marshal().json(JsonLibrary.valueOf(kind.toString()))); }
Example #20
Source File: JsonUnmarshalAction.java From syndesis-extensions with Apache License 2.0 | 5 votes |
@Override public Optional<ProcessorDefinition<?>> configure(CamelContext context, ProcessorDefinition<?> route, Map<String, Object> parameters) { ObjectHelper.notNull(route, "route"); ObjectHelper.notNull(kind, "kind"); return Optional.of(route.unmarshal().json(JsonLibrary.valueOf(kind.toString()))); }
Example #21
Source File: JsonDataformatsRoute.java From camel-quarkus with Apache License 2.0 | 5 votes |
public void configureJsonRoutes(JsonLibrary library, DataFormat dummyObjectDataFormat, DataFormat pojoADataFormat, DataFormat pojoBDataFormat) { fromF("direct:%s-in", library) .wireTap("direct:" + library + "-tap") .setBody(constant("ok")); fromF("direct:%s-tap", library) .unmarshal(dummyObjectDataFormat) .toF("log:%s-out", library) .split(body()) .marshal(dummyObjectDataFormat) .convertBodyTo(String.class) .toF("vm:%s-out", library); fromF("direct:%s-in-a", library) .wireTap("direct:" + library + "-tap-a") .setBody(constant("ok")); fromF("direct:%s-tap-a", library) .unmarshal().json(library, PojoA.class) .toF("log:%s-out", library) .marshal(pojoADataFormat) .convertBodyTo(String.class) .toF("vm:%s-out-a", library); fromF("direct:%s-in-b", library) .wireTap("direct:" + library + "-tap-b") .setBody(constant("ok")); fromF("direct:%s-tap-b", library) .unmarshal().json(library, PojoB.class) .toF("log:%s-out", library) .marshal(pojoBDataFormat) .convertBodyTo(String.class) .toF("vm:%s-out-b", library); }
Example #22
Source File: JsonDataformatsRoute.java From camel-quarkus with Apache License 2.0 | 4 votes |
@Override public void configure() { JacksonDataFormat jacksonDummyObjectDataFormat = new JacksonDataFormat(DummyObject.class); jacksonDummyObjectDataFormat.useList(); ObjectMapper jacksonObjectMapper = new ObjectMapper(); jacksonObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); jacksonDummyObjectDataFormat.setObjectMapper(jacksonObjectMapper); configureJsonRoutes(JsonLibrary.Jackson, jacksonDummyObjectDataFormat, new JacksonDataFormat(PojoA.class), new JacksonDataFormat(PojoB.class)); JohnzonDataFormat johnzonDummyObjectDataFormat = new JohnzonDataFormat(); johnzonDummyObjectDataFormat.setParameterizedType(new JohnzonParameterizedType(List.class, DummyObject.class)); configureJsonRoutes(JsonLibrary.Johnzon, johnzonDummyObjectDataFormat, new JohnzonDataFormat(PojoA.class), new JohnzonDataFormat(PojoB.class)); GsonDataFormat gsonDummyObjectDataFormat = new GsonDataFormat(); Type genericType = new TypeToken<List<DummyObject>>() { }.getType(); gsonDummyObjectDataFormat.setUnmarshalGenericType(genericType); gsonDummyObjectDataFormat.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); gsonDummyObjectDataFormat.setExclusionStrategies(Arrays.<ExclusionStrategy> asList(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getAnnotation(ExcludeField.class) != null; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } })); configureJsonRoutes(JsonLibrary.Gson, gsonDummyObjectDataFormat, new GsonDataFormat(PojoA.class), new GsonDataFormat(PojoB.class)); from("direct:jacksonxml-marshal") .marshal() .jacksonxml(true); from("direct:jacksonxml-unmarshal") .unmarshal() .jacksonxml(PojoA.class); }
Example #23
Source File: CamelStreamerMediationIngestion.java From ignite-book-code-samples with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Exception { System.out.println("Camel Streamer Mediation ingestion!"); Ignition.setClientMode(true); Ignite ignite = Ignition.start("example-ignite.xml"); if (!ExamplesUtils.hasServerNodes(ignite)) return; if (getFileLocation() == null || getFileLocation().isEmpty()){ System.out.println("properties file is empty or null!"); return; } // camel_cache cache configuration CacheConfiguration<String, String> camel_cache_cfg = new CacheConfiguration<>("camel-direct"); camel_cache_cfg.setIndexedTypes(String.class, String.class); IgniteCache<String, String> camel_cache = ignite.getOrCreateCache(camel_cache_cfg); // Create an streamer pipe which ingests into the 'camel_cache' cache. IgniteDataStreamer<String, String> pipe = ignite.dataStreamer(camel_cache.getName()); // does the tricks pipe.autoFlushFrequency(1l); pipe.allowOverwrite(true); // Create a Camel streamer and connect it. CamelStreamer<String, String> streamer = new CamelStreamer<>(); streamer.setIgnite(ignite); streamer.setStreamer(pipe); streamer.setEndpointUri("direct:ignite.ingest"); CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("file://"+getFileLocation()) .unmarshal().json(JsonLibrary.Jackson, MnpRouting.class) .bean(new RouteProcessor(), "process") .to("direct:ignite.ingest"); } }); streamer.setCamelContext(context); streamer.setSingleTupleExtractor(new StreamSingleTupleExtractor<Exchange, String, String>() { @Override public Map.Entry<String, String> extract(Exchange exchange) { String key = exchange.getIn().getHeader("key", String.class); String routeMsg = exchange.getIn().getBody(String.class); return new GridMapEntry<>(key, routeMsg); } }); streamer.start(); }
Example #24
Source File: CamelStreamerMediation.java From ignite-book-code-samples with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Exception{ System.out.println("Camel Streamer Mediation ingestion!"); Ignition.setClientMode(true); Ignite ignite = Ignition.start("example-ignite.xml"); // Create a CamelContext with a custom route that will: // (1) consume from our Jetty endpoint. // (2) transform incoming JSON into a Java object with Jackson. // (3) uses JSR 303 Bean Validation to validate the object. // (4) dispatches to the direct:ignite.ingest endpoint, where the streamer is consuming from. // camel_cache cache configuration CacheConfiguration<String, String> camel_cache_cfg = new CacheConfiguration<>("camel-direct"); camel_cache_cfg.setIndexedTypes(String.class, String.class); IgniteCache<String, String> camel_cache = ignite.getOrCreateCache(camel_cache_cfg); // Create an streamer pipe which ingests into the 'camel_cache' cache. IgniteDataStreamer<String, String> pipe = ignite.dataStreamer(camel_cache.getName()); // does the tricks pipe.autoFlushFrequency(1l); pipe.allowOverwrite(true); // Create a Camel streamer and connect it. CamelStreamer<String, String> streamer = new CamelStreamer<>(); streamer.setIgnite(ignite); streamer.setStreamer(pipe); streamer.setEndpointUri("direct:ignite.ingest"); CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("jetty:http://127.0.0.1:8081/ignite?httpMethodRestrict=POST") .unmarshal().json(JsonLibrary.Jackson) .to("bean-validator:validate") .to("direct:ignite.ingest"); } }); // Remember our Streamer is now consuming from the Direct endpoint above. streamer.setCamelContext(context); streamer.setSingleTupleExtractor(new StreamSingleTupleExtractor<Exchange, String, String>() { @Override public Map.Entry<String, String> extract(Exchange exchange) { String stationId = exchange.getIn().getHeader("X-StationId", String.class); String temperature = exchange.getIn().getBody(String.class); System.out.println("StationId:" + stationId + " temperature:" + temperature); return new GridMapEntry<>(stationId, temperature); } }); streamer.start(); //streamer.start(); }
Example #25
Source File: ResponseToJSONRoute.java From hacep with Apache License 2.0 | 4 votes |
@Override public void configure() throws Exception { from("direct:marshal-response").marshal().json(JsonLibrary.Jackson, ResponseMessage.class).convertBodyTo(String.class); }
Example #26
Source File: UserRoute.java From kubernetes-integration-test with Apache License 2.0 | 4 votes |
@Override public void configure() throws Exception { //There is no Camel retry enabled by default, but the endpoint is transacted, so redelivery is triggered by AMQ //Try transacted=false&synchronous=false to fail the testRetry test from("amq:user.in?consumerCount={{amq.consumerCount}}&transacted=true").routeId("user.in") .streamCaching() //Receive user email from body .log(LoggingLevel.DEBUG,log,"User received: ${body}") .unmarshal().json(JsonLibrary.Jackson,User.class) .to("bean-validator:user-in") //We populate this object during the route .setProperty("user",body()) //Select phone number from database .to("sql:SELECT phone FROM users WHERE email=:#${exchangeProperty.user.email}?dataSource=#dataSource&outputType=SelectOne&outputHeader=phone") .script().simple("${exchangeProperty.user.setPhone(${header.phone})}") //Call api for address .removeHeaders("*") .setBody(constant(null)) .setHeader(Exchange.HTTP_URI).constant("{{api.url}}") .setHeader(Exchange.HTTP_PATH).simple("address/email/${exchangeProperty.user.email}") .setHeader("CamelHttpMethod").constant("GET") .setHeader("Accept").simple("application/json") .to("http4:apiCall?synchronous=true") .log(LoggingLevel.DEBUG,log,"Address received: ${body}") .unmarshal().json(JsonLibrary.Jackson,Address.class) .script().simple("${exchangeProperty.user.setAddress(${body})}") //Send user with added fields to out queue .setBody(exchangeProperty("user")) .marshal().json(JsonLibrary.Jackson) .convertBodyTo(String.class) .removeHeaders("*") .log(LoggingLevel.DEBUG,log,"Send user: ${body}") .to("amq:user.out?prefillPool=false") ; }