How to Un-marshall a csv file to json file using apache camel and Spring Boot
In this tutorial we are going to learn about un-marshalling the csv file to a json file.
Firstly, Go through these tutorials for Creating MicroServices and Configuring ActiveMQ in your local machine.
Create a csv file as shown in the below image and place it under src/main/resources/repo and start the coding.
Add these dependencies to the POM.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bindy</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Create a Java class extending the RouteBuilder and start creating the routes as below.
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.spi.DataFormat;
import org.springframework.stereotype.Component;
import com.behind.java.SampleUser;
@Component
public class SampleRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
DataFormat bind = new BindyCsvDataFormat(SampleUser.class);
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(SampleUser.class);
jacksonDataFormat.useList();
jacksonDataFormat.setUnmarshalType(SampleUser.class);
from("file:src/main/resources/repo?noop=true")
.log("Data received from CSV file")
.unmarshal(bind)
.marshal().json(JsonLibrary.Jackson, true)
.convertBodyTo(String.class)
.log("${body}")
.to("direct:nextQueue")
.log("Data moved to next route");
from("direct:nextQueue")
.log("Data recieved from above route")
//uncomment this and change the file extension to csv to save it back to csv file itself
// .unmarshal(jacksonDataFormat)
// .marshal().bindy(BindyType.Csv,SampleUser.class)
.to("file:src/main/resources/repo?fileName=newSampleData.json")
.log("Successfully created JSON file");
}
}
Now create a SampleUser POJO class that holds the data received from the csv and maps it back to the json file.
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@CsvRecord(separator = ",", skipFirstLine = true, generateHeaderColumns = true)
public class SampleUser {
@DataField(pos = 1, columnName = "userId")
private String userId;
@DataField(pos = 2, columnName = "userName")
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Now run the code and see the console for log. Refresh and check the src/main/resources/repo for the file named newSampleData.json. In the similar way try saving the file to xml with the code reference.GitHub Link.