Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Contract-First API Design with Apicurio and Red Hat Fuse/Camel

July 12, 2018
Christina Lin
Related topics:
Kubernetes
Related products:
Red Hat FuseRed Hat OpenShift Container Platform

Share:

    This is part one of my two-article series that demonstrates how to implement contract-first API design using Apicurio and Red Hat Fuse.  It covers how to create an OpenAPI standard document as the contract between API providers and consumers using Apicurio Studio. It also shows how to quickly create mock tests using Red Hat Fuse which is based on Camel.

    There are two common approaches when it comes to creating APIs:

    • Code first (top-down)
    • Contract first (bottom-up)

    Code-First Approach

    To ESB developers, these two approaches aren't new. Before, it was the WSDL that defined the contract of the service. We were doing a lot more coding first, because it's easy to write a couple of Java classes and generate the WSDL for the consumers. This is the code-first approach, which has been the most common in the past.

    Code-first development can be pretty straightforward if the consumer of your application has decided how they want the service to work. There is always a preliminary discussion between the developer and consumer about the data to be exchanged. It is likely that there is some notion of the "contract" for the service, but often it is implicit. With this approach small changes are inevitable, and it takes a toll on the developer to grind through the long process of making all these updates and getting everything right. Since the contract isn't explicitly spelled out, these changes might actually break things because the developer and the consumer each have different understanding of the service's intended operation.

    .

    Contract-First Approach

    Business users and citizen users/developers can use the new OpenAPI specification to negotiate and perhaps perform a couple of pre-tests with the consumer before the design gets handed over to the developer. This design approach is called contract first. It has become more and more popular because it prevents the developer from wasting time while negotiating how the service should be provided.

    Obviously, there are many ways to implement a contract-first API. I am going to demonstrate how it can be done using Apicurio and Red Hat Fuse. I will be using Apicurio to define APIs and automatically generate the Red Hat Fuse project for the purpose of quick testing.

    Creating a Customer Service API

    In this example demo, we will be providing customer info to our consumer as a service. For the sake of this demo, we will start by retrieving and creating a customer service.

     

    Defining the Application with the OpenAPI Specification in Apicurio

    Apicurio is a web-based open source tool for designing APIs that are based on the OpenAPI specification.

    If you don't already have an Apicurio account, you need to first register for one.

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

    What you need to do next is pretty simple:

    • Create the API (service).
    • Create the data definitions (if any are required).
    • Add paths; define parameters and operations; and return responses to the paths.

    Enter the name of the service, and it would be nice to add a description for the service so it's easier for people to understand what every path means.

    Enter a customer definition to show info about what we are going exchange.

    Add and define the properties and their data type.

     
     
    Then you can start adding the paths to the document.
     
     
     
     
    Add parameters and define their type.
     
     

     

     


    Add responses, too (if your path needs them).

    Once you are done, it's time to export the API standard document.

    Generating the Red Hat Fuse Project from the Standard API Document

    Go to Red Hat Developer Studio and create a new Red Hat Fuse project by right-clicking in the navigation panel and selecting New->Fuse Integration Project.

    Provide a name for the project.

    We are going to use a microservices approach, so select the most-popular runtime—Spring Boot. We will be running this on the Red Hat OpenShift cloud platform.

    Select the Spring DSL template.

    You will then have a sample Red Hat Fuse project:

    Add the generated API specification document to the directory src/spec/.

    Edit the pom.xml file, and add the following to it:

    <plugins>
    ....
    <plugin>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-restdsl-swagger-plugin</artifactId>
      <version>2.21.0</version>
      <configuration>
        <specificationUri>src/spec/MyCustomer.json</specificationUri>
        <fileName>camel-rest.xml</fileName>
        <outputDirectory>src/main/resources/spring</outputDirectory>    
      </configuration>
    </plugin>
    ....
    </plugin>
    

    Generate the XML by running the following in the command-line tool:  

    mvn camel-restdsl-swagger:generate-xml

    You will then find a newly generated Camel context named camel-rest.xml, which has all the path implementations in Camel.

     

     

    From that file, copy everything inside the <rests> tags and paste it into the original camel-context.xml file inside camelContext. Add the following rest configuration on top of the rest block.

    <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"/>
    </restConfiguration>
     
     
     
     
     
     
     
    Delete the generated camel-rest.xml file.

    Mocking the APIs with Apache Camel

    We will mock the returned result by adding a constant, defined bean in the Camel context.

    To do that, in the src/main.resource/spring folder, add a beans.xml file by right-clicking the folder and selecting New->beans.xml File.

    Insert the following code snippet to the beans.xml file:

    <util:list id="CustomerList" list-class="java.util.ArrayList">
       <ref bean="Customer"/>
    </util:list>
    <util:map id="Customer" map-class="java.util.HashMap">
       <entry key="name" value="Christina"/>
       <entry key="age" value="28"/>
       <entry key="contact" value="765483921"/>
    </util:map>
    

    Add the Camel routes to the camel-context.xml file. The first one returns mock customer data, and the second one takes customer info as input.

    <route id="rest1-route">
     <from id="restone" uri="direct:rest1"/>
      <setBody id="route-setBody1">
        <simple>bean:CustomerList?method=get(0)</simple>
      </setBody>
    </route>
    <route id="rest2-route">
      <from id="resttwo" uri="direct:rest2"/>
      <log id="input-log" message=">>> ${body}"/>
        <setBody id="route-setBody2">
          <simple>Customer created</simple>
        </setBody>
    </route>

    Now, it's time to set up the dependency libraries in the pom.xml file:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-undertow-starter</artifactId>
    </dependency> 
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jackson-starter</artifactId>
    </dependency> 
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-swagger-java-starter</artifactId>
    </dependency>
    
    Finally, it's time to test by running the following at the command line:

    mvn sprint-boot:run

    Two endpoints will be exposed for testing:

    Christina Laptop$ curl http://YOURIP:8080/customer/id/123
    
    {"name":"Christina","age":"28","contact":"765483921"}
    
    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.

    In the next article in this series, I will take you through how to actually implement the API, expose it in the cloud, and manage it.


    For more information see these Red Hat Developer resources:

    • [DevNation Live] - Camel Riders in the Cloud - Watch the recording of Claus Ibsen,  author of the Camel in Action books.
    • Download the free ebook - Selections from Camel in Action, 2nd edition.
    • Learn more about Red Hat Fuse - a distributed, cloud-native integration solution that is based on Camel
    • Read An API Journey: From idea to deployment the Agile Way - Part 1
    Last updated: June 30, 2023

    Recent Posts

    • Cloud bursting with confidential containers on OpenShift

    • Reach native speed with MacOS llama.cpp container inference

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    What’s up next?

     

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue