Skip to content

Commit

Permalink
Add openapi contract first example
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Apr 4, 2024
1 parent fab7e0e commit 3b3141e
Show file tree
Hide file tree
Showing 9 changed files with 1,620 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ readme's instructions.
=== Examples

// examples: START
Number of Examples: 55 (0 deprecated)
Number of Examples: 56 (0 deprecated)

[width="100%",cols="4,2,4",options="header"]
|===
Expand Down Expand Up @@ -133,6 +133,8 @@ Number of Examples: 55 (0 deprecated)
| link:reactive-streams/readme.adoc[Reactive Streams] (reactive-streams) | Reactive | An example that shows how Camel can exchange data using reactive streams with Spring Boot reactor


| link:openapi-contract-first/readme.adoc[Openapi Contract First] (openapi-contract-first) | Rest | Contract First OpenAPI example

| link:platform-http/README.adoc[Platform Http] (platform-http) | Rest | An example showing Camel REST DSL with platform HTTP

| link:rest-cxf/README.adoc[Rest Cxf] (rest-cxf) | Rest | An example showing Camel REST using CXF with Spring Boot
Expand Down
4 changes: 4 additions & 0 deletions openapi-contract-first/daisy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 555,
"name": "Daisy the parrot"
}
185 changes: 185 additions & 0 deletions openapi-contract-first/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.camel.springboot.example</groupId>
<artifactId>examples</artifactId>
<version>4.6.0-SNAPSHOT</version>
</parent>

<artifactId>camel-example-spring-boot-openapi-contract-first</artifactId>
<name>Camel SB Examples :: OpenAPI Contract First</name>
<description>Contract First OpenAPI example</description>

<properties>
<category>Rest</category>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>${camel-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<!-- openapi support -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-openapi-starter</artifactId>
</dependency>
<!-- json support -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
</dependency>
<!-- camel to use spring http server -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-platform-http-starter</artifactId>
</dependency>
<!-- include camel developer console -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-console-starter</artifactId>
</dependency>
<!-- include JMX that allows additional information in camel developer console -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-management-starter</artifactId>
</dependency>
<!-- Camel CLI -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-cli-connector-starter</artifactId>
</dependency>

<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.52</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- we only want to generate the model classes for spring boot -->
<language>spring</language>
<library>spring-boot3</library>
<inputSpec>${project.basedir}/src/main/resources/petstore.json</inputSpec>
<modelPackage>sample.petstore.model</modelPackage>
<generateApis>false</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelDocumentation>false</generateModelDocumentation>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModels>true</generateModels>
</configuration>
</execution>
</executions>
</plugin>
<!-- include the model classes to source folder -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/swagger/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- spring boot plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
63 changes: 63 additions & 0 deletions openapi-contract-first/readme.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
== Camel Example OpenAPI Contract First

This example uses Camel to expose REST APIs from an existing OpenAPI specification (contract first).

From the contract we generate Java POJO classes (using maven plugin, see `pom.xml`).

In the Camel route `PetStoreRoute.java` we use Rest DSL using OpenAPI in contract-first mode.
This makes it possible to expose all the APIs very easily, and map this to corresponding Camel
routes via `direct:operationId` naming convention.

The example uses the Petstore OpenAPI example which comes with 18 APIs. This example has only
implemented 2 of these APIs, and to ignore the remaining APIs. This is handy during development,
so you can implement the APIs one by one.

=== How to run

You can run this example using

mvn spring-boot:run

=== To call the API

You can call the Rest APIs such as

----
curl http://0.0.0.0:8080/api/v3/pet/123
----

Which returns information about a pet.

You can also update an existing PET such:

----
curl -XPUT -H "Content-Type: application/json" --data "@daisy.json" http://0.0.0.0:8080/api/v3/pet
----



=== Camel CLI

This application is integrated with the Camel CLI (camel-jbang) via the `camel-cli-connector-starter` dependency (see `pom.xml`).
This allows to use the Camel CLI to manage this application, such as:

$mvn spring-boot:run

And then use the CLI to see status:

$camel get
PID NAME CAMEL PLATFORM PROFILE READY STATUS RELOAD AGE ROUTE MSG/S TOTAL FAIL INFLIGHT LAST DELTA SINCE-LAST
87918 CamelPetStore 4.6.0 Spring Boot v3.2.4 1/1 Running 0 7s 3/3 0.00 0 0 0 -/-/-

=== Help and contributions

If you hit any problem using Camel or have some feedback, then please
https://camel.apache.org/support.html[let us know].

We also love contributors, so
https://camel.apache.org/contributing.html[get involved] :-)

The Camel riders!



Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.petstore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Main class to run Spring Boot
*/
@SpringBootApplication
public class MyPetStoreApplication {

/**
* A main method to start this application.
*/
public static void main(String[] args) {
SpringApplication.run(MyPetStoreApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.petstore;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import sample.petstore.model.Pet;
import sample.petstore.model.Pet.StatusEnum;

/**
* Camel routes for the PetStore example
*/
@Component
public class PetStoreRoute extends RouteBuilder {

@Value("${petName}")
private String petName;

@Override
public void configure() throws Exception {
// turn on json binding and scan for POJO classes in the model package
restConfiguration().bindingMode(RestBindingMode.json)
.bindingPackageScan("sample.petstore.model");

rest().openApi().specification("petstore.json").missingOperation("ignore");

from("direct:getPetById")
.process(e -> {
// build response body as POJO
Pet pet = new Pet();
pet.setId(e.getMessage().getHeader("petId", long.class));
pet.setName(petName);
pet.setStatus(StatusEnum.AVAILABLE);
e.getMessage().setBody(pet);
});

from("direct:updatePet")
.process(e -> {
Pet pet = e.getMessage().getBody(Pet.class);
pet.setStatus(StatusEnum.PENDING);
});
}
}
Loading

0 comments on commit 3b3141e

Please sign in to comment.