This Week in Spring: Kubernetes, Kotlin, KNative, and More

Hi, Spring fans! Welcome to another installment of This Week in Spring! I flew 14 hours to Tel Aviv, Israel. I then spent 28 hours on the ground visiting customers, visiting my friends at Codota, and then presenting to a packed house at the Israel Java User Group. Then, I flew another 14 hours back to San Francisco, CA, where I presented for a four-hour online course on using Kotlin with Spring Boot. Tonight, I’ll fly to Atlanta, GA, for the incredible DevNexus conference where I’ll be presenting (twice) with the good Dr. Venkat Subramaniam on Kotlin (and Spring) and more. And then, I’m doing a four-hour course on testing. Join me!

And, of course, we’ve got a lot to get to today ,so let’s get to it!

How to Enrich DTOs With Virtual Properties Via Spring Projections

In JPA, as a rule of thumb, our queries (SQLs) must extract from the database only the needed data, meaning only the data that it is prone to be modified. When the fetched data is read-only (we don't plan to modify it), then we must strive to fetch it as DTOs instead of JPA entities. 

Starting from this statement, let's consider the following JPA trivial entity:

Spring Sweets: Group Loggers With Logical Name [Snippet]

Spring Boot 2.1 introduced log groups. A log group is a logical name for one or more loggers. We can define log groups in our application configuration. Then, we can set the log level for a group, so all loggers in the group will get the same log level. This can be very useful to change a log level for multiple loggers that belong together with one setting. Spring Boot already provides two log groups by default: web and sql. In the following list, we see which loggers are part of the default log groups:

  • web: org.springframework.core.codec, org.springframework.http, org.springframework.web, org.springframework.boot.actuate.endpoint.web, org.springframework.boot.web.servlet.ServletContextInitializerBeans
  • sql: org.springframework.jdbc.core, org.hibernate.SQL

To define our own log group, we must add in our application configuration the key logging.group. followed by our log group name. Next, we assign all loggers that we want to be part of the group. Once we have defined our group, we can set the log level using the group name prefixed with the configuration key logging.level..

Asynchronous RDBMS Access With Spring Data R2DBC

Not too long ago, a reactive variant of the JDBC driver was released, known as R2DBC. It allows data to be streamed asynchronously to any endpoints that have subscribed to it. Using a reactive driver like R2DBC together with Spring WebFlux allows you to write a full application that handles the receiving and sending of data asynchronously. In this post, we will focus on the database and on connecting to the database and then finally saving and retrieving data. To do this, we will be using Spring Data. As with all Spring Data modules, it provides us with out-of-the-box configuration, decreasing the amount of boilerplate code that we need to write to get our application setup. On top of that, it provides a layer upon the database driver that makes doing the simple tasks easier and the more difficult tasks a little less painful.

For the content of this post, I am making use of a Postgres database. At the time of writing only Postgres, H2 and Microsoft SQL Server have their own implementations of R2DBC drivers.

Spring Boot With Spring Data JPA

Welcome to the Spring Boot with Spring Data JPA tutorial! In this tutorial, we are going to see how Spring Data JPA provides complete abstraction over the DAO layer. We don’t need to write the implementation for the DAO layer anymore; Spring Data auto-generates the implementation DAO implementations.

We already had an introduction to Spring Boot, and for this tutorial, we will use Spring Boot along with Spring Data. You will also see how Spring Boot auto-configuration helps to get data source configurations done, hassle-free.

A Bootiful Podcast: Oleg Zhurakousky

Hi, Spring fans! In this installment, we talk to Oleg Zhurakousky, the lead of the Spring Cloud Stream project, about application integration, messaging wonk, and a prolific contributor to Spring Integration. Oleg also has the dubious honor of being one of my oldest friends on the Spring team — poor guy!

Twitter: @z_oleg
Twitter: @SpringCloud

Using ZK With Spring Boot

In recent years, Spring Boot has become a more and more popular alternative to building and deploying web applications. Following that popularity, questions arose whether existing or new ZK applications can also integrate with Spring Boot instead of building a classical war file.

The answer has always been "yes." At the same time, the transition from web.xml configuration into Java configuration has proven to be inconvenient, error-prone, and full of questions. That's why we at ZK decided to provide the zkspringboot integration module containing default and adjustable autoconfig and a starter dependency.

This Week in Spring: Spring Boot, Azure, GCP, Tips, Tutorials, and More

Hi, Spring fans and welcome to another installment of This Week in Spring! This week, I’m off to pleasant Pittsburgh, PA, to speak at, among other places, DICK’s Sporting Goods. Join me!

Now that my entire six-part series on how to use Spring Boot with Microsoft Azure just concluded, with the final parts being released last week, I wanted to give you the whole thread here for your consumption.

How to Specify Named Parameters Using the NamedParameterJdbcTemplate

Overview

Spring JdbcTemplate is a powerful mechanism to connect to the database and execute SQL queries. It internally uses JDBC API but eliminates a lot of problems of JDBC API. It helps to avoid writing boilerplate code for such as creating the connection, statement, closing resultset, connection, etc.. 

With JdbcTemple, we generally do pass the parameter values with "?" (question mark). However, it is going to introduce the SQL injection problem. So, Spring provides another way to insert data by the named parameter. In that way, we use names instead of "?". So it is better to remember the data for the column. This can be done using NamedParameterJdbcTemplate.

Magic With the Spring Boot Actuator

Spring Boot provides the Spring Boot actuator module for monitoring and managing your application when it is moved into production. Some of the production-ready features it provides are health monitoring of the application, auditing of the events, and gathering of metrics from the production environments.

For enabling the Spring Boot actuator, we need to add the following Spring Boot starter Maven dependency in pom.xml.

Deploying Spring Boot 2.x Applications in WebLogic 12.1.3.1 Using Gradle Build

Deploying Spring Boot 2.x applications in WebLogic Versions 12.2.x is simpler than deploying those in WebLogic 12.1.x using Gradle build. I was struggling with the deployment of 2.x applications in 12.1.x version of WebLogic. There is no issue if you use Maven to build. But, using the Gradle build is complicated. But, finally, after some research got the solution and deployed successfully. Today, I will demonstrate how to deploy 2.x applications in 12.1.x version of WebLogic. Let's start. For my project, I'm using below tech stack:

a. Gradle 4.5+
b. Spring Boot 2.1.1.RELEASE
c. Java 8
d. WebLogic 12.1.3.1
e. javax.servlet 4.0.1 (Excliptly defining because of older WebLogic)

Spring Boot Magic Explained: Embedded Web Servers [Video]

As soon as you develop a web application with Spring Boot, you'll run an embedded web server like Tomcat or Jetty. Embedding these web servers serves as a prime example of how Spring Boot cleverly uses conditions, dependencies (Maven or Gradle), and a bit of code to produce magic that is hard to debug if you do not know what is going on. I highly recommended that you check out the video below if you want to deepen your Spring Boot knowledge.

In this short and practical episode, you will learn:

Spring REST Service Exception Handling

This tutorial talks about Spring Rest Service Exception Handling. In our previous article, we created our very first Spring Boot REST Web Service. In this tutorial, let’s concentrate on how to handle an exception in Spring applications. While there always is an option to handle them manually and set a particular ResponseStatus, however, Spring provides an abstraction over the entire exception handling and just asks you to put a few annotations — it takes care of everything else. In this article, we will demonstrate these concepts with code examples.

How to Manually Handle Exceptions

In the Spring Boot Rest Service tutorials, we had created a Dogs Service to understand the concepts. In this post, let's extend the same Dogs Service to handle exceptions.

Set up a Spring Boot Application With PostgreSQL

In this article, we will see the steps to set up a Spring Boot application with PostgreSQL. We will have a simple CRUD operation in Postgres Database by exposing the application via Rest API. We will use POSTMAN to test the application.

Setting up Postgres Server

  • Download the Postgres server from the link: https://www.postgresql.org/download/
  • Run the installer. It will also ask the password for the superuser: postgres
  • Click on pgAdmin4.exe located inside the PostgreSQL folder inside Program Files.

Setting up Spring Boot Application

Prerequisite:

Have JDK 1.8 installed

Spring Cloud and Spring Boot, Part 2: Implementing Zipkin Server For Distributed Tracing

In my last article you have learn how to implement Eureka Server for service discovery and registration. In this article, you will learn one more important feature of microservices, Distributed Tracing.

What is Distributed Tracing?

Distributed Tracing is crucial for troubleshooting and understanding microservices. It is very useful when we need to track the request passing through multiple microservices. Distributed Tracing can be used to measure the performance of the microservices. 

Spring Cloud and Spring Boot, Part 1: Implementing Eureka Server

What is Eureka Server?

Eureka Server is service discovery for your microservices, where all client applications can register by themselves and other microservices look up the Eureka Server to get independent microservices to get the job complete.

Eureka Server is also known as Discovery Server and it contains all the information about client microservices running on which IP address and port.