org.apache.camel.component.jackson.JacksonDataFormat Java Examples

The following examples show how to use org.apache.camel.component.jackson.JacksonDataFormat. 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: AbstractContextAwareRepoEvent.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws Exception
{
    if (!isCamelConfigured)
    {
        dataFormat = new JacksonDataFormat(event2ObjectMapper, RepoEvent.class);
        configRoute();
        isCamelConfigured = true;
    }

    // authenticate as admin
    AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();

    this.rootNodeRef = retryingTransactionHelper.doInTransaction(() -> {
        // create a store and get the root node
        StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE,
            this.getClass().getName());
        if (!nodeService.exists(storeRef))
        {
            storeRef = nodeService.createStore(storeRef.getProtocol(),
                storeRef.getIdentifier());
        }
        return nodeService.getRootNode(storeRef);
    });
}
 
Example #2
Source File: JSONJacksonAnnotationsTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@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.close();
    }
}
 
Example #3
Source File: RestSwaggerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@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.close();
    }
}
 
Example #4
Source File: JsonDataformatsRoute.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@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 #5
Source File: JacksonDataFormatAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean(name = "json-jackson-dataformat-factory")
@ConditionalOnMissingBean(JacksonDataFormat.class)
public DataFormatFactory configureJacksonDataFormatFactory() throws Exception {
    return new DataFormatFactory() {
        @Override
        public DataFormat newInstance() {
            JacksonDataFormat dataformat = new JacksonDataFormat();
            if (CamelContextAware.class
                    .isAssignableFrom(JacksonDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class
                        .cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration,
                        parameters, null, false);
                CamelPropertiesHelper.setCamelProperties(camelContext,
                        dataformat, parameters, false);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            if (ObjectHelper.isNotEmpty(customizers)) {
                for (DataFormatCustomizer<JacksonDataFormat> customizer : customizers) {
                    boolean useCustomizer = (customizer instanceof HasId)
                            ? HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.json-jackson.customizer",
                                    ((HasId) customizer).getId())
                            : HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.json-jackson.customizer");
                    if (useCustomizer) {
                        LOGGER.debug(
                                "Configure dataformat {}, with customizer {}",
                                dataformat, customizer);
                        customizer.customize(dataformat);
                    }
                }
            }
            return dataformat;
        }
    };
}