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

How Spring Security's built-in filters are maintained

How is the order of built-in filters maintained in Spring Security? I think many developers are interested in this question. In this article, I will discuss this issue with you. HttpSecurity contains a member variable FilterOrderRegistration and this class is a built-in filter registry. As for the role of these filters, not the focus of this article, interested to see the FilterOrderRegistration source code. Order of built-in filters The FilterOrderRegistration

Environment in Spring detailed explanation

Environment represents the environment information for the entire spring application runtime, which contains two key elements. profiles properties profiles The concept of profiles, which I believe we all understand, is most commonly used in different environments to determine different configuration contexts for solutions in the current spring container. For example, for the development environment, test environment, production environment, build different application.properties configuration items, this time we can determine the current spring application context in effect through the property profiles.

Version control for restful api

1. Preface Software iteration is a problem that developers must face, and one issue that is easily overlooked these days is API version control. Not all users are keen on the latest version of software, and business is fluid. So when a new version is released to ensure backward compatibility, API version control is needed. Today we will discuss the common Restful API version control. 2. API Version Control Restful

Mock testing of Spring MVC interfaces

1. Preface Most of the developers contacted in Java development do not pay much attention to testing the interface, resulting in various problems in the docking. Some also use tools such as Postman for testing, although there is no problem in the use of the interface, if the interface increases the permissions testing is more disgusting. So it is recommended to test the interface in unit testing to ensure the

How to add Json Web Token in Swagger2 or Swagger3

1. Preface Swagger 3.0 has been released for some time now, and has been used by more and more projects as a very useful documentation tool. And JWT is also the most commonly used security technology for front-end and back-end separation. So how do you add JWT Token to Swagger 3.0? 2. Adding JWTs in Swagger2 Let’s first review how JWT was added in Swagger2. In Swagger2 we declare the