Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 6. Developing an application for the Spring Boot image
This chapter explains how to develop applications for the Spring Boot image.
6.1. Creating a Spring Boot 2 project using Maven archetype Link kopierenLink in die Zwischenablage kopiert!
This quickstart demonstrates how to create a Spring Boot 2 project using Maven archetypes.
Procedure
- Go to the appropriate directory on your system.
In a shell prompt, enter the following the
mvn
command to create a Spring Boot 2 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-7_13_0-00014-redhat-00001/archetypes-catalog-2.2.0.fuse-7_13_0-00014-redhat-00001-archetype-catalog.xml \ -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \ -DarchetypeArtifactId=spring-boot-camel-xml-archetype \ -DarchetypeVersion=2.2.0.fuse-7_13_0-00014-redhat-00001
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-7_13_0-00014-redhat-00001/archetypes-catalog-2.2.0.fuse-7_13_0-00014-redhat-00001-archetype-catalog.xml \ -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \ -DarchetypeArtifactId=spring-boot-camel-xml-archetype \ -DarchetypeVersion=2.2.0.fuse-7_13_0-00014-redhat-00001
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The archetype plug-in switches to interactive mode to prompt you for the remaining fields.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When prompted, enter
org.example.fis
for thegroupId
value andfuse713-spring-boot
for theartifactId
value. Accept the defaults for the remaining fields.-
If the above command exited with the BUILD SUCCESS status, you should now have a new Fuse on OpenShift project under the
fuse713-spring-boot
subdirectory. You are now ready to build and deploy the
fuse713-spring-boot
project. Assuming you are still logged into OpenShift, change to the directory of thefuse713-spring-boot
project, and then build and deploy the project, as follows.cd fuse713-spring-boot mvn oc:deploy -Popenshift
cd fuse713-spring-boot mvn oc:deploy -Popenshift
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
For the full list of available Spring Boot 2 archetypes, see Spring Boot 2 Archetype Catalog.
6.2. Structure of the Camel Spring Boot application Link kopierenLink in die Zwischenablage kopiert!
The directory structure of a Camel Spring Boot application is as follows:
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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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 Spring XML file from the classpath that contains the Camel XML DSL @ImportResource({"classpath:spring/camel-context.xml"})
@SpringBootApplication // load regular Spring XML file from the classpath that contains the Camel XML DSL @ImportResource({"classpath:spring/camel-context.xml"})
Copy to Clipboard Copied! Toggle word wrap Toggle overflow It must have a
main
method to run the Spring Boot application.Application.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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/jkube/deployment.yml
Provides additional configuration that is merged with the default OpenShift configuration file generated by the openshift-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.
6.3. Spring Boot 2 archetype catalog Link kopierenLink in die Zwischenablage kopiert!
The Spring Boot 2 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 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 Camel’s REST DSL to expose a RESTful API and expose it to 3scale. |
| Demonstrates how to use SQL via JDBC along with Camel’s REST DSL to expose a RESTful API. |
| Demonstrates how to configure Camel routes in Spring Boot via a Spring XML 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. |
| Demonstrates how to use Apache CXF JAX-RS with Spring Boot 2 on OpenShift. This quickstart uses Spring Boot2 to launch a Spring configuration file based CXF application that includes a CXF JAXRS endpoint with Swagger enabled. |
| Demonstrates how to use Apache CXF JAX-WS with Spring Boot 2 on OpenShift. The quickstart uses Spring Boot2 to launch a Spring configuration file based CXF application that includes a CXF JAXWS endpoint. |
The following Spring Boot 2 Maven archetypes fail to build and deploy on to the OpenShift. See the Release Notes for more information.
-
spring-boot-camel-archetype
-
spring-boot-camel-infinspan-archetype
-
spring-boot-cxf-jaxrs-archetype
-
spring-boot-cxf-jaxws-archetype
To work around this issue, after generating a Maven project for one of these quickstarts, edit the project’s Maven pom.xml
file to add the following dependency:
6.4. BOM file for Spring Boot Link kopierenLink in die Zwischenablage kopiert!
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.
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.
6.5. Incorporate the BOM file Link kopierenLink in die Zwischenablage kopiert!
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:
Spring Boot 2 BOM
After specifying the BOM using the dependency management mechanism, it is 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>
<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.
6.6. Spring Boot Maven plugin Link kopierenLink in die Zwischenablage kopiert!
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.
Example