Object to JSON Transfer Example Using Spring In Camel

Object to JSON Transfer Example Using Spring In Camel With Simple Steps.

In this example we will learn how can we convert Java Object into JSON using camel through spring.

  • In eclipse go to MyFirstCamelProject that we created in earlier page and right click on src/main/java to create package.New -> package

  • Give any name for example com.preparationforinterview.spring.object.json and click on Finish

  • Right click on package and create a java class for example ObjectToJsonConverter

  • Right click on src/main/resources and and create one .xml file for example ObjectToJsonSpring.xml.

  • Create two folder one with name input and other as output.At the end we will get following structure

  • Create any file in input folder.I created text file with name InputToObject.txt and added comma (,) separated value that we will use to create Object.We added below text

  • Add following dependency in pom.xml file.you can use any latest version.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  • Run mvn:install from command prompt or if you are using Eclipse right click on pom.xml and run mvn install.It will download camel jars

  • Copy below code in ObjectToJsonConverter.java

    package com.preparationforinterview.spring.object.json;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ObjectToJsonConverter {
    	public static void main(String args[]) throws InterruptedException {
    		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
    		Thread.sleep(5 * 60 * 1000);


    In this example we are loading spring file and starting CamelContext.When we start CamelContext ,it will start to read message from input and will send it to destination.

  • Create one POJO class that object we will convert into JSON
  • package com.preparationforinterview.spring.object.json;
    public class Employee {
    	private String name;
    	private double id;
    	private String departement;
    	public String getName() {
    		return name;
    	public void setName(String name) {
    		this.name = name;
    	public double getId() {
    		return id;
    	public void setId(double id) {
    		this.id = id;
    	public String getDepartement() {
    		return departement;
    	public void setDepartement(String departement) {
    		this.departement = departement;
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result
    				+ ((departement == null) ? 0 : departement.hashCode());
    		long temp;
    		temp = Double.doubleToLongBits(id);
    		result = prime * result + (int) (temp ^ (temp >>> 32));
    		result = prime * result + ((name == null) ? 0 : name.hashCode());
    		return result;
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Employee other = (Employee) obj;
    		if (departement == null) {
    			if (other.departement != null)
    				return false;
    		} else if (!departement.equals(other.departement))
    			return false;
    		if (Double.doubleToLongBits(id) != Double.doubleToLongBits(other.id))
    			return false;
    		if (name == null) {
    			if (other.name != null)
    				return false;
    		} else if (!name.equals(other.name))
    			return false;
    		return true;
    	public String toString() {
    		return "Employee [name=" + name + ", id=" + id + ", departement="
    				+ departement + "]";
  • We will create one Processor using that we will take value from InputToObject.txt and will create Employee Object.Create one processor say MyProcessor
    package com.preparationforinterview.spring.object.json;
    import org.apache.camel.Exchange;
    public class MyProcessor implements org.apache.camel.Processor{
    	public void process(Exchange exchange) throws Exception {
    		String input = exchange.getIn().getBody(String.class);
    		String args [] = input.split(",");
    		Employee employee = new Employee();
  • Write lines in ObjectToJsonSpring.xml.

    	<beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
    	<bean id="myprocessor" class="com.preparationforinterview.spring.object.json.MyProcessor" />
    	<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    			<json id="json" library="Jackson" />
    			<from uri="file:input?noop=true" />
    			<process ref="myprocessor"/>
    			<marshal ref="json" />
    			<to uri="file:output" />


    Last entry in xml file <camelContext xmlns="http://camel.apache.org/schema/spring"/> creates SpringCamelContext which provides CamelContext.In CamelContext we have add route so when we start spring application that routes also start. It will take input from input folder and will call our processor that creates Employee Object and put in route again.That object is converted into equivalent JSON and sent it to output route.

    To check the output right click on ObjectToJsonConverter.java and run it. It will copy output in output folder from input folder
    To run the program from command prompt,open command prompt and go to project location and run below command.
    mvn compile exec:java -Dexec.mainClass=com.preparationforinterview.spring.object.json.ObjectToJsonConverter

Download source code

Object to JSON Transfer Example Using Spring In Camel

Visit Others Links

Camel Introduction
Camel Setup
File Copy Example Using Camel
File Copy Example In Camel Using Spring
JMS Message Transfer Example Using Camel
JMS Message Transfer Example In Camel Using Spring
Processor In Camel
Object to JSON Transfer Example Using Spring In Camel
Camel With Bean Injection Example Using Spring DSL
Camel With Bean Injection Registry Example Using Java DSL
Camel Bean Parameter Binding Example Using Java DSL
Camel Error Handling With Redelivery Example Using Java DSL
Camel Exception Handling With Example Using Java DSL
Camel JUnit Test With Example Using Java DSL
Camel JUnit Test With Example Using Spring DSL
Camel Unit Test With Mock Component Example
Camel With Stream Component Example
Camel In-Memory Messaging Example Using Direct
Camel With Timer And Quartz Example
Camel Aggregator EIP Example
Camel Splitter EIP Example
Camel Routing Slip EIP Example
Camel Dynamic Router EIP Example
Camel Load Balancer EIP Example
Top Camel Interview Question
© 2019 PreparationForInterview.com