Skip to main content

Fuse - Contract First API Design with Apicurio and Fuse/Camel - Part One

This is part one of my two-article series that demonstrates the approach of implementing contract-first API design using Apicurioand Red Hat Fuse.

It covers how to create an OpenAPI standard document as the contract between API providers and consumers using Apicurio. It also shows how to quickly create mock tests using Red Hat Fuse.

There are two common approaches of creating these APIs.
  • Code First
  • Contract First
Coming from a old time ESB developer, these are not new. We have been doing this forever. Before, it was the WSDL that define the contract of the service. we were doing a lot more code first, for me it's simply because it's much easier for me to write couple of Java classes and generate the WSDL for my consumer. 

It's often pretty straightforward if the consumer of your application has finalized how they want the service to be like. But you and I all know this is not often the case. So I had to go back to my code, and make the changes accordingly and pray I did not break anything in the way.

Of course there is always preliminary discussion, but small changes are inevitable, and it's taking the toll of developer to grind through the process of getting everything right.

Thanks to the new OpenAPI Specification, business user, citizen user/developer can use it to negotiate and maybe rung a couple of pre-test with the consumer before it gets handed over to developer. This design approach is called Contract First. It has become more and more popular as it avoids developer wasting time while in the process of negotiating how the service should be provided.

Obviously there are many ways to do Contract First API . I am going to demonstrate how this can be done using Apicurio and Fuse. I will be using Apicurio to define APIs, and automatically generates the Fuse project for quick testing purpose.

Theme: Customer Service API

We will be providing customer service to our consumer. For the sake of this demo, we will only start with retrieving and creating customer service.

Defining Application with OpenAPI Specification in Apicurio

Apicurio is a web-based Open Source tool to design OpenAPI specification. Go to

If you don't already have an account, please register for one.

After registering, you will be redirected to the main page of Apicurio, so after discussing with the consumer on what they wish to have for the customer service, we can start creating the contract by clicking on "Create New API".

Basically, what you need to do is pretty simple

  • Create the API(Service)
  • Create the data definitions (if any) 
  • Add Paths, and define parameters, operations and return responses to the path

Enter the name of the service, and would be nice to add some of the description to it so it's easier for people to pick up what everything means.

Set the customer definition, show the detail of info we are going exchange. 

Add and define the properties and their data type.

We can start adding the Paths to the doc, 

Adding Parameters and define it's type.

And responses too, (if your path needs it)

Once we are done, it's time to export the API Standard documents.

Generating Fuse Project from Standard API Doc

Go to JBoss Developer Studio, create a new Fuse project by right click on navigation panel, select New and Fuse Integration project.

Provide a name to the project,

We are going to do this in a microservices approach, so select the most popular runtime - Spring Boot, and we will be running this on Cloud Platform - OpenShift.

Select the Spring DSL template.

You will have a sample Fuse project,

Add the generated API Spec doc to directory: src/spec/

Go to the pom.xml file, place the following in it,



Generate the XML by running following in command line tool,

  mvn camel-restdsl-swagger:generate-xml

You will find a newly generated camel context named "camel-rest.xml", and it has all the Path implementation in Camel.

Copy everything in between "rests", into the original "camel-context.xml" and place in-between "camelContext". and add the following rest configuration on top of the rest component.

<restConfiguration apiContextPath="api-docs" bindingMode="auto"
            component="undertow" contextPath="/customer"
            enableCORS="true" port="8080">
   <apiProperty key="cors" value="true"/>
   <apiProperty key="api.title" value="Customer Service"/>
   <apiProperty key="api.version" value="1.0.0"/>


Delete the generated "camel-rest.xml". 

Mocking the APIs with Camel

We will mock the return result by adding a constant defined bean in the camel context.
Add a beans.xml under src/main.resource/spring (Right click on the folder, select New->beans.xml File)

Add the follow code snap to the beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xsi:schemaLocation="      ">
<util:list id="CustomerList" list-class="java.util.ArrayList">
   <ref bean="Customer"/>
<util:map id="Customer" map-class="java.util.HashMap">
   <entry key="name" value="Christina"/>
   <entry key="age" value="28"/>
   <entry key="contact" value="765483921"/>


Add the camel route in the to "camel-context.xml", the first one returns a mock customer data, the second one takes the input of a customer info.

<route id="rest1-route">
  <from id="restone" uri="direct:rest1"/>
  <setBody id="route-setBody1">
<route id="rest2-route">
  <from id="resttwo" uri="direct:rest2"/>
  <log id="input-log" message=">>> ${body}"/>
    <setBody id="route-setBody2">
      <simple>Customer created</simple>


And in the src/main/java/org/mycompany/ added the new beans.xml file.


public class Application {

Now, it's time to setup the dependency libraries into pom.xml.



and remove the tomcat dependency (we are using undertow now)


Finally, it's time to test. run the follow in command line:

  mvn spring-boot:run

And two endpoint will be exposed for testing.

Christina Laptop$ curl http://YOURIP:8080/customer/id/123

Christina Laptop$ curl --header "Content-Type: application/json"   --request PUT   --data '{"name":"Christina","age":28,"contact":"765483921"}'   http://YOURIP:8080/customer/add

"Customer created"

You are now ready for the consumer to start testing the APIs.
Next part I will take you through how to actually implement the API, expose it on Cloud and manage it.


Unknown said…
Hello! This is my first visit to your blog! This is my first comment here, so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. Your blog provided us useful information. You have done an outstanding job.
malaysia visa singapore

Puremelda said…
Find a company that offer best custom essay writing service that guarantees you research competence. A company that provides you with only custom term paper writing service with exemplary research abilities, and also endow them with an assortment of research facilities.
Priya said…
This is really wonderful information, it really helpful for learners. Thanks for posting
Mulesoft Online Training
Mulesoft Training in Hyderabad

Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
mulesoft online training
best mulesoft online training
top mulesoft online training

Popular posts from this blog

Red Hat Fuse - Announcing Fuse 7 Tech preview 3 release.

Red Hat Fuse 7.0 technical preview three is out today! On the pathway to become one of the best cloud-native integration platform, Fuse gives developer freedom to choose how they want to develop the integration solution, where they want to deploy it and capabilities to address new integration personas that do not have development experience.
By supporting the three major runtime, developer is free to work on the runtime of their choice.By supporting standalone and cloud deployment, it simplifies the complexity to distinguish between these environments, allowing application to deploy freely among the environment of your choice. All levels of developers are welcome, you can either dive deep into creating customize complex integration logic, or using the new low code platform to quickly build a simple integration. In this Tech Preview release you get it all.
Fuse StandaloneSpring-boot for microserviceKaraf 4 for OSGi loverJBoss EAP for JavaEE developersFuse on OpenShiftPlugins for easy co…

JBoss EAP 6 - 效能調校 (一) DataSource 的 Connection Pool

效能沒有什麼Best Practice, 反正能調整的就那些。 通常,一個程式的效能大概有70-80% 都跟程式怎麼寫的其實比較有關係。

最近我最疼愛的小貓Puji 因為膀胱結石開刀的時候過世了,心情很差請原諒我的口氣沒有很好,也沒有心情寫部落格。

Puji R.I.P.



JBoss 的 SubsystemDatasource WebWeb Service EJB Hibernate JMSJCAJVM 調校OS (作業系統)

先來看一下 DataSource Subsystem, DataSource 的部分主要是針對Connection Pool 做調校。

通常,程式都會需要跟資料庫界接,電腦在本機,尤其是在記憶體的運算很快,但是一旦要外部的資源連接,就是會非常的耗資源。所以現在的應用程式伺服器都會有個Pool 放一些先連接好的 資料庫connection,當程式有需要的時候就可以馬上提供,而不用花那些多餘的資源去連接資料庫。

這就是為什麼要針對Connection Pool 去做調校。

以下會討論到的參數,都是跟效能比較有關係,Datasource 還有很多參數,像是檢核connection 是否正確的,我都不會提到。如果你追求的是非常快速的效能,那我建議你一個檢核都不要加。當然,這樣就會為伺服器上面執行的程式帶來風險。這就是你要在效能與正確,安全性上面的取捨了。 (套句我朋友說的話,不可能又要馬兒好,又要馬兒不吃草的..)

最重要的調校參數就是 Connection 的 Pool 數量。(也就是那個Pool 裡面要放幾條的connection.) 這個參數是每一個應用程式都不一樣的。


Connection Pool 最少會存留的connection 數量


Connection Pool 最多可以開啓的 connection 數量


事先將connection pool 裡面建立好min-pool-size 的connection.

我的建議是觀察一下平常程式要用到的量設定為 min-pool-size 。

My 2cents on the future of Integration - With Service Mesh/Istio and Serverless/KNative

It's been a year and half since I blogged about "Agile Integration architecture" (Gosh, time just flies). With the "microservices" and "cloud-native" hype, I was especially curious on how all these new concept and technology affect us on how to architect the integration systems. If you ever pay close attention to all the latest and greatest news from the Kubernetes community, I am sure you will hear a lot about the new "Service Mesh". And rumor has it that this is how integration can/should be done in cloud native world, but, is that so? Anyone who has ever worked on an integration project would tell you, it's a LOT more COMPLEX and can get worst overtime. I did a talk with Christian Posta in Red Hat Tech Exchange coming from a more comprehensive view of how different Red Hat technologies are applied under different patterns when building integration solutions. In fact he also did a great blog about it.

Since then, another topics has be…