Chapter 5. Developing an Application for the Spring Boot Image
This chapter explains how to develop applications for the Spring Boot image.
5.1. Creating a Spring Boot Project using Maven Archetype
To create a Spring Boot project using Maven archetypes follow these steps.
Procedure
- Go to the appropriate directory on your system.
In a shell prompt, enter the following the
mvn
command to create a Spring Boot project.mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \ -DarchetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/2.2.0.fuse-740017-redhat-00003/archetypes-catalog-2.2.0.fuse-740017-redhat-00003-archetype-catalog.xml \ -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \ -DarchetypeArtifactId=spring-boot-camel-xml-archetype \ -DarchetypeVersion=2.2.0.fuse-740017-redhat-00003
The archetype plug-in switches to interactive mode to prompt you for the remaining fields.
Define value for property 'groupId': : org.example.fis Define value for property 'artifactId': : fuse74-spring-boot Define value for property 'version': 1.0-SNAPSHOT: : Define value for property 'package': org.example.fis: : [INFO] Using property: spring-boot-version = 1.5.17.RELEASE Confirm properties configuration: groupId: org.example.fis artifactId: fuse74-spring-boot version: 1.0-SNAPSHOT package: org.example.fis spring-boot-version: 1.5.17.RELEASE Y: :
When prompted, enter
org.example.fis
for thegroupId
value andfuse74-spring-boot
for theartifactId
value. Accept the defaults for the remaining fields.- Follow the instructions in the quickstart on how to build and deploy the example.
For the full list of available Spring Boot archetypes, see Spring Boot Archetype Catalog.
5.2. Structure of the Camel Spring Boot Application
The directory structure of a Camel Spring Boot application is as follows:
├── LICENSE.md ├── pom.xml ├── README.md ├── configuration │ └── settings.xml └── src ├── main │ ├── fabric8 │ │ └── deployment.yml │ ├── java │ │ └── org │ │ └── example │ │ └── fis │ │ ├── Application.java │ │ └── MyTransformer.java │ └── resources │ ├── application.properties │ ├── logback.xml │ └── spring │ └── camel-context.xml └── test └── java └── org └── example └── fis
Where the following files are important for developing an application:
- pom.xml
-
Includes additional dependencies. Camel components that are compatible with Spring Boot are available in the starter version, for example
camel-jdbc-starter
orcamel-infinispan-starter
. Once the starters are included in thepom.xml
they are automatically configured and registered with the Camel content at boot time. Users can configure the properties of the components using theapplication.properties
file. - application.properties
Allows you to externalize your configuration and work with the same application code in different environments. For details, see Externalized Configuration
For example, in this Camel application you can configure certain properties such as name of the application or the IP addresses, and so on.
application.properties
#spring.main.sources=org.example.fos logging.config=classpath:logback.xml # the options from org.apache.camel.spring.boot.CamelConfigurationProperties can be configured here camel.springboot.name=MyCamel # lets listen on all ports to ensure we can be invoked from the pod IP server.address=0.0.0.0 management.address=0.0.0.0 # lets use a different management port in case you need to listen to HTTP requests on 8080 management.port=8081 # disable all management endpoints except health endpoints.enabled = false endpoints.health.enabled = true
- Application.java
It is an important file to run your application. As a user you will import here a file
camel-context.xml
to configure routes using the Spring DSL.The
Application.java file
specifies the@SpringBootApplication
annotation, which is equivalent to@Configuration
,@EnableAutoConfiguration
and@ComponentScan
with their default attributes.Application.java
@SpringBootApplication // load regular Blueprint file from the classpath that contains the Camel XML DSL @ImportResource({"classpath:blueprint/camel-context.xml"})
It must have a
main
method to run the Spring Boot application.Application.java
public class Application { /** * A main method to start this application. */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
- camel-context.xml
The
src/main/resources/spring/camel-context.xml
is an important file for developing application as it contains the Camel routes.NoteYou can find more information on developing Spring-Boot applications at Developing your first Spring Boot Application
- src/main/fabric8/deployment.yml
Provides additional configuration that is merged with the default OpenShift configuration file generated by the fabric8-maven-plugin.
NoteThis file is not used part of Spring Boot application but it is used in all quickstarts to limit the resources such as CPU and memory usage.
5.3. Spring Boot Archetype Catalog
The Spring Boot Archetype catalog includes the following examples.
Name | Description |
---|---|
| Demonstrates how to use Apache Camel with Spring Boot based on a fabric8 Java base image. |
| Demonstrates how to connect a Spring Boot application to an ActiveMQ broker and use JMS messaging between two Camel routes using Kubernetes or OpenShift. |
| Demonstrates how to configure a Spring Boot application using Kubernetes ConfigMaps and Secrets. |
| Demonstrates how to use Apache Camel to integrate a Spring Boot application running on Kubernetes or OpenShift with a remote Kie Server. |
| Demonstrates how to connect a Spring Boot application to a JBoss Data Grid or Infinispan server using the Hot Rod protocol. |
| Demonstrates how to use SQL via JDBC along with Camel’s REST DSL to expose a RESTful API. |
| Spring Boot, Camel and XA Transactions. This example demonstrates how to run a Camel Service on Spring Boot that supports XA transactions on two external transactional resources: a JMS resource (A-MQ) and a database (PostgreSQL). This quickstart requires the PostgreSQL database and the A-MQ broker have been deployed and running first, one simple way to run them is to use the templates provided in the Openshift service catalog |
| Demonstrates how to configure Camel routes in Spring Boot via a Blueprint configuration file. |
| Demonstrates how to use Apache CXF with Spring Boot based on a fabric8 Java base image. The quickstart uses Spring Boot to configure an application that includes a CXF JAXRS endpoint with Swagger enabled. |
| Demonstrates how to use Apache CXF with Spring Boot based on a fabric8 Java base image. The quickstart uses Spring Boot to configure an application that includes a CXF JAXWS endpoint. |
A Technology Preview quickstart is also available. The Spring Boot Camel XA Transactions quickstart demonstrates how to use Spring Boot to run a Camel service that supports XA transactions. This quickstart shows the use of two external transactional resources: a JMS (AMQ) broker and a database (PostgreSQL). You can find this quickstart here: https://github.com/jboss-fuse/spring-boot-camel-xa.
Technology Preview features are not supported with Red Hat production service level agreements (SLAs), might not be functionally complete, and Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process. For more information, see Red Hat Technology Preview features support scope.
5.4. BOM file for Spring Boot
The purpose of a Maven Bill of Materials (BOM) file is to provide a curated set of Maven dependency versions that work well together, preventing you from having to define versions individually for every Maven artifact.
Ensure you are using the correct Fuse BOM based on the version of Spring Boot you are using (Spring Boot 1 or Spring Boot 2).
The Fuse BOM for Spring Boot offers the following advantages:
- Defines versions for Maven dependencies, so that you do not need to specify the version when you add a dependency to your POM.
- Defines a set of curated dependencies that are fully tested and supported for a specific version of Fuse.
- Simplifies upgrades of Fuse.
Only the set of dependencies defined by a Fuse BOM are supported by Red Hat.
5.4.1. Incorporate the BOM file
To incorporate a BOM file into your Maven project, specify a dependencyManagement
element in your project’s pom.xml
file (or, possibly, in a parent POM file), as shown in the examples for both Spring Boot 2 and Spring Boot 1 below:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project ...> ... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- configure the versions you want to use here --> <fuse.version>7.4.0.fuse-sb2-740019-redhat-00005</fuse.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>fuse-springboot-bom</artifactId> <version>${fuse.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ... </project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project ...> ... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- configure the versions you want to use here --> <fuse.version>7.4.0.fuse-740036-redhat-00002</fuse.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>fuse-springboot-bom</artifactId> <version>${fuse.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ... </project>
After specifying the BOM using the dependency management mechanism, it becomes possible to add Maven dependencies to your POM without specifying the version of the artifact. For example, to add a dependency for the camel-hystrix
component, you would add the following XML fragment to the dependencies
element in your POM:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-hystrix-starter</artifactId> </dependency>
Note how the Camel artifact ID is specified with the -starter
suffix — that is, you specify the Camel Hystrix component as camel-hystrix-starter
, not as camel-hystrix
. The Camel starter components are packaged in a way that is optimized for the Spring Boot environment.
5.5. Spring Boot Maven plugin
The Spring Boot Maven plugin is provided by Spring Boot and it is a developer utility for building and running a Spring Boot project:
-
Building — create an executable Jar package for your Spring Boot application by entering the command
mvn package
in the project directory. The output of the build is placed in thetarget/
subdirectory of your Maven project. -
Running — for convenience, you can run the newly-built application with the command,
mvn spring-boot:start
.
To incorporate the Spring Boot Maven plugin into your project POM file, add the plugin configuration to the project/build/plugins
section of your pom.xml
file, as shown in the following example:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project ...> ... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- configure the versions you want to use here --> <fuse.version>7.4.0.fuse-740036-redhat-00002</fuse.version> </properties> ... <build> <plugins> <plugin> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${fuse.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ... </project>