by BehindJava

How to Un-marshall a JSON and XML message to a Java Bean on ActiveMQ camel route using Spring Boot

Home » springboot » How to Un-marshall a JSON and XML message to a Java Bean on ActiveMQ camel route using Spring Boot

In this tutorial we are going to learn about Un-marshalling JSON and XML message to a Java Bean on a ActiveMQ camel route using spring boot.

Firstly, Go through these tutorials for Creating MicroServices and Configuring ActiveMQ in your local machine.

Once the MicroServices and ActiveMQ is up and running. We will move files from a MicroService a to the ActiveMQ and MicroService b will consume the JSON and XML message and Un-marshall it into a local Java Bean.

Create two folders named json and xml under the files folder in MicroService a as shown below. If you have any doubt about how the files folder is created, refer this tutorial on file transfer using camel route.

MicroService a

packageexporler

package com.example.apachemsa.routes;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class FileRouter extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("file:files/json").log("${body}").to("activemq:file-queue");
	}
}

Have these configuration in the application.properties file present under src/main/resources.

spring.activemq.broker-url=tcp://localhost:61616

Once we place the json files in the above path i.e., files/json camel will load them into the queue and MicroService b consumes it and in the MicroService b we will Unmarshall the Json file given below to a local Java bean.

Sample Json file

{
    "fx_id": 111,
    "from": "INR",
    "to": "USD",
    "conversionRate": 70
}

Sample XML file

{
    <?xml version="1.0" encoding="UTF-8"?>
<root>
   <fx_id>1000</fx_id>
   <from>USD</from>
   <to>INR</to>
   <conversionRate>70</conversionRate>
</root>
}

MicroService b

Create a class named FileRouterReceiver in MicroService b and also a bean class with getters and setters, add the dependency in the pom.xml.

JSON Dependency

<dependency>
			<groupId>org.apache.camel.springboot</groupId>
			<artifactId>camel-jackson-starter</artifactId>
			<version>3.14.1</version>
		</dependency>

XML Dependency

<dependency>
			<groupId>org.apache.camel.springboot</groupId>
			<artifactId>camel-jacksonxml-starter</artifactId>
			<version>3.14.1</version>
		</dependency>

Java class

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.springframework.stereotype.Component;

@Component
public class FileRouterReceiver extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("activemq:file-queue")
        .unmarshal()
		.json(JsonLibrary.Jackson, FxConverter.class)
		//For mapping XML to the bean
		//.jacksonxml(FxConverter.class)
		.to("log:recieving-mesages-activemq");

	}
}

Bean class

import java.math.BigDecimal;

public class FxConverter {
	private Long fx_id;
	private String from;
	private String to;
	private BigDecimal conversionRate;

	public FxConverter() {

	}

	public FxConverter(Long fx_id, String from, String to, BigDecimal conversionRate) {
		super();
		this.fx_id = fx_id;
		this.from = from;
		this.to = to;
		this.conversionRate = conversionRate;
	}

	public Long getFx_id() {
		return fx_id;
	}

	public void setFx_id(Long fx_id) {
		this.fx_id = fx_id;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public BigDecimal getConversionRate() {
		return conversionRate;
	}

	public void setConversionRate(BigDecimal conversionRate) {
		this.conversionRate = conversionRate;
	}

	@Override
	public String toString() {
		return "FxConverter [fx_id=" + fx_id + ", from=" + from + ", to=" + to + ", conversionRate=" + conversionRate
				+ "]";
	}
}

Have these configuration in the application.properties file present under src/main/resources.

application.properties

server.port=8000
spring.activemq.broker-url=tcp://localhost:61616

Once you run the MicroService b on the ActiveMQ and STS console you will see the following.

activemq

INFO 3696 --- [mer[file-queue]] recieving-mesages-activemq               : Exchange[ExchangePattern: InOnly, BodyType: com.example.apachemsb.routes.FxConverter, Body: FxConverter [fx_id=111, from=INR, to=USD, conversionRate=70]]

This is how we push messages or files to the Queue and map or Un-marshall it to the local bean using camel framework.