What are classes for the Spring JDBC API

Spring Boot Tutorial: Building Java Apps with Spring Boot

Since its release in 1995, Java has been one of the most popular and widely used programming languages. Especially when it comes to the Realization of complex business applications (online or offline), the object-oriented language shows its strengths. In combination with the Java runtime environment (JRE), these applications are characterized above all by their platform independence, which makes them available to a particularly wide audience.

To the Simplify work with Java, various frameworks have been developed over the past few decades to give users a ready-to-use basic framework for the Java program to be developed. One of the best known is the open source framework Spring, published in 2003, which significantly reduces the complexity of the standard Java specification (“J2EE” or “Java Platform”) and the component model Enterprise JavaBeans (“EJB”). At the latest since the release of the addition Spring Boot, which additionally simplifies the configuration of new Spring projects, the framework has proven to be an excellent and user-friendly choice for everyone who does not want to start from scratch when developing a new Java application.

In this Spring Boot tutorial you will learn which requirements must be met for use and how you can cope with the first steps with the modular framework and the Spring Boot attachment.

What is Spring Boot?

Spring Boot is a “convention over configuration” solution for the Spring Java framework that was released in 2012 and is the Reduced the complexity of configuring new Spring projects. For this purpose, Spring Boot defines a basic configuration including guidelines for the use of the framework as well as all relevant third-party libraries and thus provides the way to make the entry into new projects as easy as possible. In this way, the creation of independent, production-ready applications based on Spring can be considerably simplified, which is why the Most of the new Spring applications consequently also touches down on Spring Boot.

The Features of Spring Boot can be summarized as follows:

  • direct Embed from web server / container applications such as Apache Tomcat or Jetty possible, which means that no use of WAR files (W.eb A.pplication Archive) is required
  • simplified Maven configuration thanks to "starter" POMs (P.roject Object M.odels)
  • automatic Spring configurationwhenever possible
  • Provision non-functional features such as metrics or outsourced configurations

With Spring Boot, the developer company Pivotal Software has given the framework, which was released in 2005, a modern, future-oriented approach. The extension benefits from the Basic technology of the Spring framework that has matured over the years, which has also played a key role in Spring Boot. You can find detailed information on the open source framework (Apache License 2.0) itself in our detailed article on the Spring Framework.

Spring Boot Tutorial: Requirements, Installation, and Getting Started

Since Spring Boot was added to Spring, Pivotal Software's software has been recognized as one of the best solutions for developing microservices in Java. thanks to the around 20 modules of the basic framework, which can be put together at will, Spring Boot is also suitable for various other scenarios, whereby it can neither be specifically classified as a framework for web applications nor as a classic solution for desktop apps.

Before we go into more detail in the following Spring Boot tutorial (German) on the configuration and creation of a new project and the initial Spring Boot structure, we will first deal with the technical requirements and the setup of the necessary project components.

What are the system requirements for Spring Boot?

Spring Boot is all about Java, which is why the Java Runtime Environment (Java Platform) is the central software component to get the framework up and running. Since you not only run the application, but also use it for development with Java, you need at least version 8 (or 1.8) of the so-called Java Development Kit (JDK). In addition to the runtime environment, this contains useful tools for programming and testing Java applications. It is available for Linux as well as for Windows and macOS, regarding the operating system so have you free choice.

Of course, the extension also requires that the current version of the Spring framework is installed on the system itself.

Either come as a build tool Maven (from 3.3) or Gradle (from 4.4) in question.

If you want to make use of the possibility of integrating a web server that executes your Java application on a servlet basis, you can choose between three solutions ApacheTomcat (from 9.0), Jetty (from 9.4) and Undertow (from 2.0).

Here's how to create your first Spring Boot project

Use Spring Boot just like any standard Java library by using the appropriate JAR- (Java Archive) or WAR files (Web Application Archive) Include in the classpath. Java uses this path in the file system to search for executable files. You can create the archive files for Spring Boot in two different ways:

  • You install and use Maven or Gradleto create the project framework including the necessary dependencies entirely on your own.
  • You grab the Web service Spring Initializr back, compile your Spring Boot setup with this and then download it as a finished project template.

The special feature of the latter solution is that you can save the JAR files via a user-friendly web interface that simplifies the process significantly. Since the initializr also uses Maven or Gradle to generate the files, the result does not differ from that of the manual method. As part of this Java or Spring Boot tutorial, we have therefore opted for the web assistant, which can be called up via the address start.spring.io.

If you decide to use Spring Initializr, Java and Maven or Gradle must also be installed so that you can work on your Spring Boot application.

Once you have opened Spring Initializr, you can define the desired properties of your first Spring Boot application one after the other: First, define the build system by choosing “Maven Project"Or"Gradle Project" choose. The second line gives you the option of choosing classic Java or, alternatively, another language of the Java family such as Kotlin or Groovy. You also define the underlying Spring Boot version, the metadata of the project (including the Java version) and all relevant dependencies (databases, security features, web interfaces, cloud services, etc.). By clicking on "Generate Project“Finally create the project files.

If you want to set up your Spring Boot project on your own with Maven and Gradle, you can use the installation instructions in the official online manuals as a guide.

Run the created Spring Boot project

Spring Initializr offers you the generated project files for download, whereby these are initially in a ZIP archive that you have to unzip after downloading. In the unzipped folder you will find among other things. the respective configuration file: Do you have Gradle selected for build management, it is the file build.gradle; at Maven projects you should see the file there pom.xml find. In the latter case, which also applies to our approach in this Spring Boot tutorial, the content of the configuration file looks something like the following:

With the help of the respective build software, you can create a executable spring boot application. To do this, open Terminal or Command Prompt and run the following command in the target folder (i.e. in the folder of your Spring Boot files) if you have Maven use:

For Gradle projects is the command however as follows:

In both cases you will receive the message "BUILD SUCCESS“As soon as the archive files have been created successfully. The executable file is saved in the folder "target"(Maven) or under"build / libs“(Gradle) filed. In our case, the name of this JAR file is "demo-0.0.1-SNAPSHOT.jar“:

You can run it using Java by typing the following command:

The one in italics in the code example for clarity Name of the JAR file it applies accordingly to replaceif this differs from the one mentioned here. Based on the output then displayed in the command line, you can now see whether the Spring Boot application could be started successfully. If you have integrated one of the mentioned web servers such as Tomcat, it will also be started. You can address him with the browser of your choice by calling up the following address:

Connect Spring Boot to the database and data source

Spring Boot offers first-class support for the creation and integration of data sources in any SQL or NoSQL databases. You don't have to write any additional code to create a new data source, just add the required dependencies and configuration details. This is made possible by the uniform database interface JDBC (Java D.atabase C.onnectivity), for which there is initially a Starter entry in the configuration file applies if you have not already added the dependency when creating the project with Spring Initializr (the same applies to adding other named dependencies).

Maven users add to this in the pom.xml the following lines:

Gradle user complete this step by adding the following entry to the build.gradle-File:

You can open the respective configuration file with any standard code editor. It is important that the file is saved again in XML or Gradle format after the changes.

In the second step, add the dependency for the desired database management system to the respective build configuration file. In this Spring Boot tutorial connect we our application with a MySQL databasethat we want to use for our application and have already installed.

The pom.xml of Maven projects requires the following new entry:

In the Gradle configuration the following line must be added to create the connection to the MySQL database:

Then some properties have to be added in the PROPERTIES file "application". This file can be found under the path \ src \ main \ resources in the Spring Boot project directory. Open this text file and insert the following configuration lines:

Now you can use MySQL in the usual way to create a Database including the desired Create data tables - if you have not already done so. Beginners can find detailed information on using the relational database management system in our extensive MySQL tutorial.

Generate SSL / TLS certificate and activate HTTPS

By default, a Spring Boot application that has been created uses the unsecured HTTP protocol and TCP port 8080 for the connection to the respective web server. To do that instead secured HTTPS and the ones provided for this TCP port 443 To make it the default choice, you need an SSL / TLS certificate and the appropriate properties in the "application" PROPERTIES file.

You can obtain the former either from a certification authority or by contacting a self-signed certificate using the Java runtime environmentcreate. With the Management Utility Key Tool, this has a command line tool ready with which you can easily create PKCS12 files (contain X.509 certificate and private key) for servers such as Tomcat, Exchange, IIS etc. For example, if you have a Tomcat server enter the following command at the command prompt or in the terminal to open the Keystore file for your Spring Boot application to generate:

 

To Personalization of the certificate Then assign an individual password and provide some information about yourself, your company and your location:

The keystore file is then automatically stored in the directory in which you executed the command (in this Spring Boot tutorial: in your own user directory). Copy the "keystore" file into the directory of your Spring Boot application and add the following lines to the "application" -PROPERTIES file required for the database integration:

Finally, create a new executable JAR file that connects to Tomcat via HTTPS as configured using the following command:

Simplify debugging the Spring Boot app with Spring Cloud Sleuth and Zipkin

The constant testing is one of the most important tasks in software development, because this is the only way to locate and correct all errors that arise during programming. However, the evaluation of log files is usually very complex, which is what the Debugging process is often very time-consuming power - especially when it comes to microservices. At the end of the Spring Boot tutorial, we therefore want to set up the tracing solutions Spring Cloud Sleuth and Zipkin in order to monitor the Spring Boot application all around.

With the help of the two tracing applications, you can easily track all requests via a previously set up REST controller and display the chronological sequence at any time.

Step 1: add Spring Cloud Sleuth to the project

First integrate the Sleuth module into your Spring Boot application by setting the dependency in the build configuration file. To do this, add the following content to the pom.xml one if you Maven use:

In the configuration file of Gradle projects it is necessary to add the following line:

Step 2: Integrate sleuth logs into the class file of the REST controller

The REST controller of your Spring Boot application must now be configured in such a way that it processes Sleuth's log files as desired. Add the .classFile of the controller, add the following configuration text for this purpose - the package name (here spring-boot-tutorial) corresponds to the domain of the in the pom.xml:

Step 3: List the name of the Spring application in the properties

To ensure that the name of the Spring Boot application is displayed correctly in Zipkin, you should enter the name in the "application"-Specify PROPERTIES file. If you save the configuration on a Spring Cloud configuration server, the same has to be done in the PROPERTIES file "bootstrap":

Step 4: add Zipkin to the project

Zipkin helps to monitor and manage the sleuth logs, whereby the open source software consists of a user interface and a server component. For both, dependencies are added in the respective build configuration file. Maven user put the following content in the pom.xml a:

In the Gradle configuration the new lines look like this:

Step 5: Enable the application to act as a Zipkin server

The next step is to ensure that your Spring Boot project can act as a Zipkin server. To do this, create a file named zipkin.properties and insert the following lines there:

Then activate the server component in the project's Java file:

Create the executable JAR file in the usual way and then run the application, the Zipkin server is at the address localhost / zipkin to reach.

Step 6: add Spring Cloud Zipkin dependency

Add one final dependency in the build config file to link Sleuth and Zipkin.

Maven:

Step 7: Configure the Sleuth log export to the Zipkin server

Add the AlwaysSampler object (bean) to the Java file of your Spring Boot application in order to activate the automatic export of the Sleuth logs to the Zipkin server:

Step 8: Enter the base URL of the Zipkin server in "application" -PROPERTIES

In the last step, enter the base URL of the Zipkin server in the application properties by opening the PROPERTIES file of your Spring Boot project again and adding the following line:

Similar articles

Apache Hadoop: distributed storage architecture for large amounts of data

Would you like to carry out complex computing processes with large amounts of data? This is the specialty of the big data framework Hadoop. The Apache software with open source status offers a Java-based framework with which various big data applications can be parallelized on computer clusters. It shines with its modular expandability, which offers an enormous range of functions for the user ...

Web frameworks - overview and classification

Web engineering can no longer be imagined without frameworks. Instead of writing applications from scratch, programmers can rely on proven program frameworks from predefined classes and methods for object-oriented software development. However, choosing a suitable web framework is far from easy. We explain how such a framework works ...

Spring - the framework for complex Java applications

Java does not play a major role in simple web development, although the programming language certainly has its appeal. The language, which was once supported by browsers, has established itself as an efficient solution, especially when it comes to the design of backends for business applications. The Spring framework contains various modules that serve as the basis for complex web and desktop applications and that ...

CodeIgniter - the lightweight among the PHP frameworks

CodeIgniter is an open source PHP framework developed by EllisLab that is currently under the patronage of the British Columbia Institute of Technology. The lean software is based on the MVC architecture pattern, is praised for its good performance and impresses with its detailed documentation. We provide the structure, structure and application flow of the CodeIgniter framework ...

What is WebSocket?

Typically, a client-server connection is carried out in a question-and-answer pattern using HTTP. When new data is to be loaded, a request is made from the client to the server. This checks the request and sends a response to the client. Only now does the data become visible. With WebSocket as a bidirectional connection, this process can be much faster, even without ...