- Build your own devon4j application
In this chapter we are going to show how to create a new devon4j application from scratch. The app that we are going to create is Order Service, a simple to order items. You can read all the details of the app in the Order Service page.
The recommended way to start developing devon4j applications is to get the last devon-ide to have a ready-to-start development environment.
You should get from your trainer instruction how to prepare the environment.
Once the configuration is completed you will get the distribution’s content.
Open a command shell of your choise (CMD, ConEMU, PowerShell, Windows Terminal, in the workspaces\main directory in your devon-ide installation and type)
$ devon
devonfw-ide environment variables have been set for C:\projects\jbf-training in workspace mainPlease fork the repository https://github.com/java-backend-foundations/devonfw-java-backend-training into your Github account and clone it into workspaces\main. Therefore, run the following command in the console to change to the directory to workspaces\school
$ cd workspaces\main
$ git clone https://github.com/<github id>/devonfw-java-backend-training.git
$ cd devonfw-java-backend-trainingIn order to create a new application, you must use the archetype provided by devon which uses the maven archetype functionality.
There are two alternatives for using the archetype to create a new application. One is to create using command line. Another way is within eclipse, which is a more visual manner.
To create a new application, you need to execute following commands:
$ mvn archetype:generate -DarchetypeVersion=3.0.1 -DarchetypeGroupId=com.devonfw.java.templates -DarchetypeArtifactId=devon4j-template-server -DgroupId=com.devonfw.app.java -DartifactId=order-service -Dversion=0.0.1-SNAPSHOT -Dpackage=com.devonfw.app.java.order -DdbType=h2This will create a new directory inside workspaces\main\devonfw-java-backend-training with the name of your application with the created application inside.
Compile the source using the mvn command
$ cd order-service
$ mvn clean installTo create a new application using Eclipse, you should have devon-ide installed. Please open command shell in workspaces\main and start Eclipse using
$ devon
devonfw-ide environment variables have been set for C:\projects\jbf-training in workspace main
devon eclipseThen, follow below steps to create a new application:
Go to File > New > Maven Project. If you don’t see the option, click File > New > Other and use the filter to search the option Maven Project
Set workspaces\main\devonfw-java-backend-training\order-service as the location
In the New Maven Project wizard, you need to choose the devon4j-template-server archetype, as shown in below image. Please uncheck the option Show the last version of Aechetype only and chose the version 3.0.1 of the archetype.
|
Note
|
If you are not able to access the archetype, then try adding the archetype repository manually. You can do it with the Configure button located next to the Catalogs dropdown and then clicking the Add Remote Catalog button. Finally, you need to add the repository URL https://repo1.maven.org/maven2 and as Description you can use Maven Central. Use the Verify button to check the connection. Subsequently, you will see a message with the amount of found archetypes. |
Fill the Group Id, Artifact Id, Version and Package for your project. Also in Properties available from archetype section update the dbtype parameter
-
Group Id:
com.devonfw.app.java -
Artifact Id:
order-service -
Version:
0.0.1-SNAPSHOT -
Package:
com.devonfw.app.java.order -
dbtype:
h2
Click on the Finish button and the project will be ready for execution.
|
Note
|
You can skip this step if you have created the project using the Eclipse. The project was automatically imported into Eclipse |
As last step we can import the project we just created into the Eclipse IDE. Although our new devon4j based app is still empty we are going to show how to run it with Spring Boot simply to check that everything is ok.
Please open command shell in workspaces\main and start Eclipse (if not done yet) using
$ devon
devonfw-ide environment variables have been set for C:\projects\jbf-training in workspace main
devon eclipseNow import our new project with File > Import.
Select Maven/Existing Maven Projects
Browse for the order-service directory. Next select the api and core projects (you will not need the other projects)
Click Finish and wait while the dependencies of the project are resolved to complete the import process.
Creating devon4j based apps we get the following main features out-of-the-box:
-
Maven project with api project, core project and server project:
-
api project for the common API
-
core project for the app implementation
-
server project ready to package the app for the deployment
-
-
Data base ready environment with an h2 instance
-
Data model schema
-
Mock data schema
-
Database version control with Flyway
-
Bean mapper ready
-
Basic security enabled (based on Spring Security)
-
Unit test support and model
Now let’s change the server context path of our application. Open core/src/main/resources/config/application.properties and set the server.context-path property to /order-service
server.servlet.context-path=/order-service|
Note
|
You can also change the port where the application will be available with the property |
The generated project is per default configured to use the H2 file database. Please change the configration to use the in-memory database. Please change replace following line
spring.datasource.url=jdbc:h2:./.order-service;with
spring.datasource.url=jdbc:h2:mem:order-servicePlease add the DevTools dependency into the pom.xml file of the core project
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>DevTools provides Spring developers with some handy development-time tools. Among those are
-
Automatic application restart when code changes
-
Automatic browser refresh when browser-destined resources (such as templates, JavaScript, stylesheets, and so on) change
-
Automatic disable of template caches
-
Built in H2 Console if the H2 database is in use
|
Note
|
To be able to correctly display the H2 Console you need to perform small change in the Spring Security configuration. Please open the .headers().frameOptions().sameOrigin().and() |
Finally, using Spring Boot features (that provides us with an embedded Tomcat), we can run the app in an easy way. Look for the SpringBootApp.java class and click right button and select Run As > Java Application.
If everything is ok you will see a messages in the Console window like
2020-11-13 11:45:57.648 INFO 27448 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '/order-service' 2020-11-13 11:45:57.651 INFO 27448 --- [ restartedMain] c.devonfw.app.java.order.SpringBootApp : Started SpringBootApp in 12.141 seconds (JVM running for 14.109)
The app will be available at http://localhost:8081/order-service
|
Note
|
You are redirected to the login screen because, by default, the new devon4j applications provide a basic security set up. |
Please use the waiter/waiter credentials to login. You should see following content
The generated project provides under http://localhost:8081/order-service/services/rest/security/v1/currentuser a simple REST service to receive the current logged-in user. Try to call the service using Postman
Instead of receiving the current user data we get a response with the login form. This is because the devon4j applications, by default, implements the Spring Security so we would need to log in using the log in form to access to the services.
To ease the example we are going to change the login method to Http Basic Authentication and disable the CSRF filter (otherwise you would need to send additional CSRF token with each writting operation)
In the file BaseWebSecurityConfig.java edit the configure(HttpSecurity http) method and replace following line:
.csrf().requireCsrfProtectionMatcher(new CsrfRequestMatcher()).and()with
.csrf().disable().httpBasic().and()Now run again the app and try again the same call. You should obtain the data of the current user
During the training you will probably see the content of the database. Please open the H2 Console using following url http://localhost:8081/order-service/h2-console/ Please use the correct JDBC URL you have configured previously.
After successful login you should see following content
We will generate some parts of the application using Cobigen. The first time we use Cobigen, it is recommended to check the health of the tool. To do so, right-click over an entity and select Update Templates… and next Health Check
The next dialogs will show us if there are outdated templates. In that case we can solve it clicking the Update button.
If you get following error, Eclipse needs the Cobigen templates to be imported. The simplest way is to click the Adapt Templates… button. Cobigen will download and import actually used templates.
Perform the health check again. You can also perform the advanced check.




















