While using Spring Cloud Config Server, we also have the feature to encrypt sensitive information that is otherwise stored as plain text in our external git repository. This prevents anyone who has read access to the external repository from accessing confidential information and is highly highly recommended if you are using a Spring Cloud Config Server.
Note: If you haven’t set up Spring Cloud Config Server yet check out my other article on how to set up a spring cloud config server.
Spring Cloud Config Server: Step by Step
Spring Cloud Config Server is used to provide server-side and client-side support for externalized configuration in a distributed system. So when you have multiple microservices, and you want to easily control the configuration for all of them at one go - you’ll mostly be looking at Spring Cloud Config Server.
So how do we do this? We will create a git project which contains all your properties files for the multiple microservices that you have (easy enough).
Spring Data JPA Query Method by Multiple Columns Example
In this tutorial, we will learn how to write a Spring Data JPA query method or finder method for multiple columns/fields.
Consider the following Product entity class and if we want to retrieve products by their name OR description fields then here is the Spring data JPA query method:
1 public List<Product> findByNameOrDescription(String name, String description); Let’s create a complete example to understand end to end.
Maven Dependencies Create a Spring boot project and add the following maven dependencies to it:
Spring Boot Integration Testing MySQL CRUD REST API Tutorial
In this tutorial, we will learn how to perform Integration testing Spring boot application using @SpringBootTest annotation.
We use MySQL database to store and retrieve the data.
Let’s first take a look at the overview of @SpringBootTest annotation.
@SpringBootTest Annotation Spring Boot provides @SpringBootTest annotation for Integration testing. This annotation creates an application context and loads the full application context.
@SpringBootTest will bootstrap the full application context, which means we can @Autowire any bean that’s picked up by component scanning into our test.
URL Rewriting With Spring Cloud Gateway
1. Introduction A common use case for the Spring Cloud Gateway is to act as a facade to one or more services, thus offering clients a simpler way to consume them.
In this tutorial, we’ll show different ways to customize the exposed APIs by rewriting the URLs before sending the request to the backends.
2. Spring Cloud Gateway Quick Recap The Spring Cloud Gateway project is built on top of the popular Spring Boot 2 and Project Reactor, so it inherits its main treats:
Automatic decoding of request bodies via RequestBodyAdvice
The client encodes or encrypts the request body (e.g. AES encryption, Base64 encoding), and the server decodes the request body after receiving it. This is a very common requirement. Thanks to the RequestBodyAdvice interface provided by spring mvc. We can do this very easily and without modifying any code in the Controller. Practice The client’s request body is encoded using Base64 and the server automatically decodes it via RequestBodyAdvice. This
Monitoring spring boot application logs online via WebSocket
This article will teach you how to monitor spring boot application’s live logs online via websocket.
Create a project poml The spring-boot-starter-websocket dependency needs to be added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 <project xmlns="http://maven.
Use Aop + SpEl to record more detailed request logs
Spring powerful expressions (SpEL) , you can dynamically access the properties of Java objects , call the object’s methods . In the request logging through Aop with SpEL can make the log information more detailed and flexible.
This article will not explain the detailed techniques related to spel and aop. If you are not familiar with them, you can visit the official documentation to learn them.
Practicing OperationLog Handler methods annotated with @OperationLog will be logged with detailed access logs.
How to Record Request and Response Bodies in Sping Boot Applications
In some scenarios, we need to log every client request and response log (including detailed body information).
As you know, streams can only be read once. If we read the request body first in order to log it, then spring’s HttpMessageConverter will throw an exception when it reads the request body again: org.springframework.http.converter. HttpMessageNotReadableException: Required request body is missing: ....
The following example demonstrates this situation.
The case of HttpMessageNotReadableException Filter for logging 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package io.
Improve file download efficiency with Zero Copy
Zero-Copy The “zero” of zero-copy means that the number of times data is copied between user and kernel states is zero.
Traditional data copying (file-to-file, client-to-server, etc.) involves four user-state to kernel-state switches and four copies. two of the four copies between user-state and kernel-state require CPU participation, while two copies between kernel-state and IO devices do not require CPU participation in DMA mode. Zero-copy avoids copying between user and kernel states (2 times) and reduces the switching between user and kernel states twice, so the data transfer efficiency is high.
Dynamically modifying Logger log levels in Spring Boot applications
Spring Boot uses logback as the logging framework by default. logback provides an interface to get logger and modify logger logging levels.
Actually actuator also provides endpoint for managing logs. You can check the official documentation for details.
LogController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 package io.
Using commons-exec to backup MYSQL database in spring boot application
mysqldump is a database backup program provided by mysql. It dumps the data stored in the database into SQL statements that are output to standard output.
Basic Usage:
1 mysqldump -u[username] -p[password] [database] > [sql file] The > symbol is a redirector under linux, redirecting the standard output to a file.
mysqldump You can refer to the official documentation for more detailed usage.
Use in spring boot applications Start a new subprocess via commons-exec to execute mysqldump to complete the backup.
Get all uploaded files in SpringBoot application
File uploads generally use a request body in multipart/form-data format. The code to handle uploading files in Spring Boot is as follows:
1 2 3 4 5 @PostMapping(path = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public void upload(@RequestParam("file1") MultipartFile file1, @RequestParam("file2") MultipartFile file2) throws IOException, ServletException { // Processing of uploaded files... } The code above the upload method declares 2 parameters (including the form name of the file), representing 2 files. If we don’t know how many files are uploaded and the name of the file form.
Deep Dive Into Saga Transactions With Kafka Streams and Spring Boot
In this article, you will learn how to use Kafka Streams and Spring Boot to perform transactions according to the Saga pattern. To be honest, I was quite surprised by a great deal of attention to my last article about Kafka. I got some questions about streams, transactions, and support for Kafka in Spring Boot. In this article, I’ll try to answer a few of them. I will also show how you can easily set up a cloud-managed Kafka on the Upstash.
A Guide to JUnit5 Lifecycle Methods
In this tutorial, you will learn about JUnit5 Lifecycle methods. A Lifecycle Method is any method that is directly annotated or meta-annotated with @BeforeAll, @AfterAll, @BeforeEach, or @AfterEach. Let’s have a look at these annotations one by one.
@BeforeAll When a method is annotated with @BeforeAll it means that the method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class.
@BeforeEach When a method is annotated with @BeforeEach it means that the method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class.
Spring Boot Quick Integration With Mybatis Framework
Mybatis is a popular orm framework. It can maintain SQL statements in XML and is very flexible. This is the key for many developers to choose it as an orm framework.
Official Website: https://mybatis.org/mybatis-3/
Github: https://github.com/mybatis/mybatis-3
Mvnrepository: https://mvnrepository.com/artifact/org.mybatis/mybatis
1 2 3 4 5 6 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> Using mybatis in spring boot applications is easy. Only a few steps are required.
This article focuses on “How to integrate mybatis in spring boot”.
Solve the problem of static files in SpringBoot applications garbled
For text resources in the SpringBoot static directory, if they contain Chinese, the Chinese in the content will be garbled when accessed directly through the browser. This is despite the fact that the files themselves are UTF8 encoded. Demo Prepare a static resource file - foo.js The content of the file contains Chinese 1 2 3 4 5 /* SpringBoot中
Running the Spring Boot application as a Linux system service
I’m working on a legacy project today. There is a Spring Boot project deployed on a physical server on the customer’s intranet, and recently, for some reason, the server has been rebooting frequently. It’s a pain to manually start the Spring Boot project after each reboot, so I’m going to configure it to turn on a self-starting service. This was done with the help of systemd. It provides a solution
SpringBoot 2.6.x coping strategies after disabling cyclic dependencies by default
1. Preface SpringBoot 2.6.x does not recommend the use of circular dependencies, which is good news, SpringBoot is gradually guiding developers to write standard code from the bottom up, but also sad news, circular dependencies are too widely used.
If you upgrade from a low version to 2.6.x, then the first problem you are likely to encounter is a circular dependency problem.
2. Problem recovery 2.1. Code description The following style of code is more common: both classes have the need to call each other’s methods, so it is easy to write a circular reference.
Spring Security will soon deprecate the configuration class WebSecurityConfigurerAdapter
Anyone who has used WebSecurityConfigurerAdapter knows that it is very important for Spring Security, it manages the whole Spring Security configuration system. But soon this class will be obsolete, you read it right, this class will be marked by @Deprecated in version 5.7 and this class will be removed in the future(#10822). Since this configuration class will be deprecated soon, there must be a transition plan. I’ve actually written an