Implement Generic Service Java
Prerequisites
Java SDK
A Java 11 SDK is needed to develop Generic Service Projects (Java).
Please download either the Oracle 11 JDK from https://www.oracle.com/java/technologies/downloads/#java11 or the Open JDK from https://openjdk.java.net/ and follow the installation instructions of the JDK.
Maven
Maven version >=3.6.1 is required to develop custom implementation Java based projects.
Please download and install maven from https://maven.apache.org.
IDE
We recommend using Eclipse in the latest version which can be downloaded from: https://www.eclipse.org/downloads/
Eclipse plug-ins
We recommend installing the following Eclipse plug-ins and utilities.
Editorconfig (https://marketplace.eclipse.org/content/editorconfig-eclipse)
Download dependencies
In order to build and run Generic Service Projects (Java), some Solution Envoy dependencies are required that come with many already configured tools.
Open the Solution Envoy dashboard (you can find the link in Solution Designer on the CI/CD page after you have set up at least one deploy pipeline or inside Solution Hub after selecting a k5-project and opening the tab " Service Deployments").
Navigate to the Infrastructure page and download all the dependencies listed under Maven Dependencies (Java).
Install dependencies
The downloaded dependencies must be installed in the local maven repository. Here is an example how the dependencies can be installed via maven.
mvn install:install-file -Dfile=cp-framework-sdk-autoconfiguration-x.y.z.jar -DpomFile=cp-framework-sdk-autoconfiguration-x.y.z.pom
mvn install:install-file -Dfile=cp-framework-sdk-parent-x.y.z.pom -DpomFile=cp-framework-sdk-parent-x.y.z.pom
Running / building projects
Before you can start working on your project you have to create the project in Solution Designer and download all necessary artifacts and dependencies.
Create a new project
Open Solution Designer and create a new project by clicking on the Create button and select Generic Service Projects (Java). Provide additional information required and click on Create. Once this is created the project should appear in the Solution Designer. Optional: Open that project in Solution Designer and write a proper documentation.
Clone Git project to local machine
In order to build and run the project, the Git project needs to be cloned to the user's workspace. To check out a project, follow the steps below:
Open the project in Solution Designer
In the bottom of the page there is the status bar. Click on Git
Copy the Git repository URI.
Open a terminal and clone the project. You will be prompted for a password. Please use your personal access token as password:
git clone <URI>
Open project
Open your project folder using Eclipse, IntelliJ or preferred IDE.
Open project with Eclipse
You can use the Import Wizard to import Domain Service Projects (Java) into your workspace.
Launch Eclipse.
Open or create a Workspace.
From the main menu bar, select command link File > Import.... The Import wizard opens.
Select General > Projects from Folder or Archive and click Next.
Click the Directory button on the next page of the wizard to select the directory the name of the project acronym.
Under Folder select all projects.
Click Finish to start the import.
The imported project named project acronym-application is the working project it is automatically recognized and created as a Maven project. Alternatively, only the project acronym-application directory can be imported.
Open project with Eclipse
You can use the Welcome screen to import Domain Service Projects (Java).
Launch IntelliJ IDEA.
If the Welcome screen opens, click Open.
Otherwise, from the main menu, select File | Open.
In the dialog that opens, select the pom.xml file from the directory with the name of the project acronym-application and click Open.
Alternatively, project acronym can be opened and Maven set up.
Local Setup
Create an
application-local.yaml
file and override properties needed for the local profile. As a template you can use the fileapplication-local.template.yaml
.Depending on which configuration approach from above add either your OpenShift k5-project namespace or a dummy namespace mock value
de: knowis: cp: consumer: kubernetes: namespace: "namespace-name"
Load MongoDB configuration from k8s configuration map
feature: mongo: enabled: true #mongoDb configuration from k8s secret spring: data: mongodb: secretName: "k5-default-document-storage-service-binding"
To access local MongoDB, you can set the
spring.data.mongodb.uri
property to change the URL and configure additional settings such as the replica set, as shown in the following example:feature: mongo: enabled: true spring: data: mongodb: database: <database> uri: mongodb://<username>:<secret>@<host>:<port>
Alternatively, you can specify connection details using discrete properties. For example, you might declare the following settings as shown below:
spring: data: mongodb: host: <host> port: <port> database: <database> username: <username> password: <password>
Alternatively, you can also disable MongoDB if it is not needed. See Disabling MongoDB.
Load event topic binding configuration from k8s secret(s)
You need to add your k5-project name in step 2.
You need to have proper
oc proxy
andoc port forwarding
to be able to access your OpenShift k5 project event topic binding secret(s) and their configured Kafka cluster.
feature: kafka-events: enabled: true #kafka configuration from k8s secret de: knowis: cp: consumer: kafka: binding: secretName: "k5-default-kafka-binding"
Alternatively, you can configure to use your Kafka brokers.
Install and configure your Kafka broker. See https://kafka.apache.org/quickstart.
The mandatory parameters for the Kafka events Configuration
feature: kafka-events: enabled: true de: knowis: cp: consumer: kafka: binding: kafka_brokers_sasl: "<kafka SASL>" user: "" password: ""
Note: Kafka brokeruser
andpassword
are optional if your Kafka broker is not secured.The following parameters can be optionally changed.
de: knowis: cp: consumer: kafka: binding: securityProtocol: SASL_SSL saslMechanism: SCRAM-SHA-512 sslProtocol: TLSv1.2 sslEnabledProtocols: TLSv1.2 sslIdentificationAlgorithm: HTTPS saslJaasConfigLoginModuleQualifiedName: org.apache.kafka.common.security.scram.ScramLoginModule kafka_custom_config: <kafka_custom_config>
To configure Swagger UI for local development, use the settings below:
feature: openapi: enabled: true webmvc: enabled: true springdoc: swagger-ui: oauth: clientId: <client-id>
If you have
spring-security-oauth2-client
on your classpath, you can take advantage of some autoconfiguration to set up an OAuth2/Open ID Connect client, as shown in the following example:feature: security: enabled: true spring: security: oauth2: client: provider: <provider-name> default: redirect-uri: <redirect-url> registration: default: client-id: <client-id>
Build and run the code. Pass the configuration below in
jvm
arguments to use the local profile.-Dspring.profiles.active=local
Disabling MongoDB
You can disable MongoDB if its not needed using below steps
Disable MongoDB feature flag.
feature:
mongo:
enabled: false
Remove the DbService.java, DBServiceTest.java
Remove DBService dependency from HelloWorldController.java , HelloWorldControllerTest.java
In your application entry point exclude MongoAutoConfiguration.class from spring boot application context
@SpringBootApplication(exclude = MongoAutoConfiguration.class) public class SOMEApplication { ... }
Unit tests for Generic Service Project (Java)
It's highly recommended writing unit tests for your Java projects. The unit tests will be executed during the project pipeline execution as part of the maven build.
By default, JUnit 5 is set as the test library in Spring Boot. If you want to use JUnit 4, you have to add the following dependency in pom.xml.
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Disable git-commit-id-plugin
for local development (optional)
You can disable the git-commit-id-plugin
for local development, if you experience a negative performance impact:
Open
{USER_HOME}/.m2/settings.xml
Add following profile with any unique id, e.g.,
gitcommitidskip
<settings ...> <!-- ... --> <profiles> <!-- ... --> <!-- New profile gitcommitidskip start --> <profile> <id>gitcommitidskip</id> <activation> <property> <name>!skipgitcommitidskip</name> </property> </activation> <properties> <maven.gitcommitid.skip>true</maven.gitcommitid.skip> </properties> </profile> <!-- New profile gitcommitidskip end -->