File Downloading in Spring Boot Applications

This article provides guidance on how to download a single file, download a Gzip-compressed file, and download multiple files through a zip archive in a Spring Boot application. Download a single file The key points are as follows. Get the size of the file. Get the media type (Content-Type) of the file. Construct the Content-Disposition header with the ContentDisposition utility class to avoid the problem of downloading file name gibberish. Copy data to the client.

Inventory of Spring Security framework in the eight classic design patterns

1. Template method pattern The template method pattern is an abstract class that overtly defines a template for executing its methods. Its subclasses can override the method implementation as needed, but the invocation will be made in the same way as defined in the abstract class, which is a behavioral pattern. The advantages of the Template method are as follows. The public part of the code is extracted in the parent class, which is easy to reuse and extend the code.

Customizing Jackson in Spring Boot2 Applications

Overview This article was originally written to understand how Spring Boot2 specifically serializes and deserializes the JSR 310 datetime system, Spring MVC application scenarios are as follows. using @RequestBody to read the JSON request body from the client and encapsulate it into a Java object. use @ResponseBody to serialize the object into JSON data and respond to the client. For some basic types of data like Integer, String, etc., Spring MVC can solve it with some built-in converters without user concern, but for datetime types (e.

Comparison and application of Spring AOP and AspectJ

1. Introduction AOP, or aspect-oriented programming is a very common technique, especially in Java Web development. And the most popular AOP frameworks are Spring AOP and AspectJ respectively. 2. Spring AOP vs AspectJ Spring AOP is based on the Spring IoC implementation, which addresses most common requirements, but it is not a complete AOP solution. It has even less to offer for objects that are not managed by the Spring container.

Migrating Spring Cloud Eureka Registry to K8s

Background Recently, I was responsible for migrating our Spring Cloud microservices running on AliCloud ECS to k8s. To ensure smoothness, we still need to keep the Eureka system in k8s, and will not consider removing Eureka until all services are running in k8s. The implementation process can be roughly divided into two phases: pilot and full-scale rollout. In the pilot phase, some independent peripheral services are migrated to k8s to observe the operation.

Spring WebFlux supports Spring Security for JWT authentication

Mobile projects, authentication requirements are relatively simple, Spring Cloud Gateway is only responsible for JWT verification and role authentication, login and so on are all custom processing, microservices pass JWS to achieve the purpose of passing credentials, downstream services do not need to authenticate and do not rely on Spring Security, the code that requires the current user directly resolve JWS to get the current user There is also the fact that WebSecurityConfigurerAdapter has been marked as deprecated in Spring Security 5.

javax.inject.Named is not available after upgrading to Spring Boot 3

To keep up with Spring 6, Spring Boot released 3.0.0 on November 24, 2022. The current version is 3.0.1 (2022-12-23). Spring 6 requires JDK 17+, and naturally Spring Boot 3 requires JDK 17+ to work. For those who have been clinging to JDK 8, upgrading to Spring Boot 3 is a big challenge. What are the significant features that Spring Boot brings to the table? Relies on Spring 6, requires Java 17 minimum, Java 19 compatible support for generating GraalVM native images, replacing the experimental Spring Native project requires Java EE 9 and supports Jakarta EE 10 as a minimum dependency migration from Java EE to Jakarta EE API Upgrade to Tomcat 10 For guidance on upgrading from Spring Boot 2.

Talking about the push and pull model of service discovery

Preface In the past year, my focus has been on API gateway (AliCloud CSB), which is a new area for me, but inseparable from my previous exposure to microservices governance. API gateways for microservices scenarios require some basic capabilities, one of which is to connect to registries that serve as entry points for microservices. For example, Zuul and SpringCloud Gateway have implemented such a feature. In fact, many open source gateways have major limitations in this feature, so I will not discuss these limitations in this paper, but share my thoughts on it for the common scenario of service discovery.

GraalVM and Spring Native Tasting, Launching SpringBoot Projects in 66ms

Introduction GraalVM is a high-performance JDK that supports Java/Python/JavaScript and other languages. It allows Java to be turned into a binary file for execution, making programs run faster anywhere. This may be a war between Java and Go? Download and install GraalVM Install GraalVM First of all, go to the official website to download, I downloaded it directly from GitHub Release Page, please download the corresponding system package, I downloaded the following.

Implementing declarative HTTP calls using the @HttpExchange annotation

In the Spring Cloud family, inter-process communication can be done using RestTemplate or OpenFeign. (Of course there are other ways such as message-driven microservices based on messaging middleware or gRPC-based calls). RestTemplate can be treated as a common HTTP calling tool, and is particularly convenient for calling RESTful-style interfaces, as opposed to other HTTP clients. However, more convenient than RestTemplate is OpenFeign, through the interface declaration can achieve remote calls, the specific use of these in the previous article, here will not repeat.

Using native image in spring boot3

Introduction As we introduced in the previous spring boot3 article, one of the important features of spring boot3 is the support for compiling spring boot3 applications into GraalVM Native Images. Today we use a specific example to show you how to properly compile a spring boot3 application into a native image. Install GraalVM If you want to compile spring boot3 app as a native application, you need the support of GraalVM.

Java JDK Proxy and CGLib Dynamic Proxy

Introduction The proxy pattern has many application scenarios in Java, and there are static code and dynamic proxies. Static proxies are implemented by weaving in code at writing, compiling or loading time, while dynamic proxies are implemented at runtime. In simple terms, static proxies exist before runtime, while dynamic proxies exist at runtime. And there are two common implementations of dynamic proxies. JDK Proxy: JDK Proxy comes with the JDK and does not require the introduction of external libraries and is proxied by implementing interfaces.

Uploading folders in Spring Boot applications

Learn how to upload folders in a spring boot application with this article. HTMLInputElement.webkitdirectory In the browser, we usually select the file to be uploaded by the <input type="file"/> tag. By default, it can only select one file or multiple files, not the whole folder directly. If the <input/> tag has an attribute called webkitdirectory, the user can select the entire folder and the browser will upload all the files under the folder to the server at once.

New and enhanced features in Spring Framework 6

On November 16, 2022, Juergen Hoeller made the announcement that Spring Framework 6 will be officially released. Spring 6 is a major release, more than four years after the release of Spring 5. Let’s take a quick look at the exciting features in the Spring 6 release in this article. JDK 17+ and Jakarta EE 9+ baseline Entire framework code base now based on Java 17 source level. Servlet, JPA, etc.

Spring Cache Redis uses SCAN instead of KEYS to match the key to be evicted in bulk

When spring-cache uses redis as the cache implementation, if the cache is deleted in bulk via @CacheEvict(allEntries = true), the KEYS command of redis is used by default to match the keys to be deleted. Example of using KEYS Define a cache implementation class that removes all eligible caches via the @CacheEvict(allEntries = true) annotation. 1 2 3 4 5 6 7 8 import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Component; @Component public class FooCache { @CacheEvict(cacheNames = "app::cache", allEntries = true) public void clear () {}; } Run the test method, execute the clear method, and observe the output log.

Spring Boot 3 Upgrade Guide

Spring Boot 3.0 is now available and includes more than 5,700 code commits from 151 developers over 12 months. This is the first major revision of Spring Boot since the release of 2.0 4.5 years ago. It is also the first Spring Boot GA release to support Spring Framework 6.0 and GraalVM, and the first Java 17-based version of Spring Boot, benchmarked against Jakarta EE 9 and supporting Jakarta EE 10.

Spring Framework 6.0 official GA, the beginning of a new generation of frameworks

Spring Framework 6.0.0 is now available. This is the start of a new framework generation for 2023 and beyond, embracing current and upcoming innovations in OpenJDK and the Java ecosystem. At the same time, we carefully designed it as a straightforward upgrade from Spring Framework 5.3.x for modern-day runtime environments. As a major revision to the core framework, Spring Framework 6.0 brings the Java 17+ baseline and migration to Jakarta EE 9+ (in the jakarta namespace) with a focus on the recently released Jakarta EE 10 APIs such as Servlet 6.

Spring Native builds native images without Graal's maven plugin buildtools

Create a Spring Boot project Create a Spring Boot project and add Spring Native dependencies, we are mainly using the AOT functionality provided by Spring Native. As follows. 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 <dependencies> <dependency> <groupId>org.springframework.experimental</groupId> <artifactId>spring-native</artifactId> <version>${spring-native.version}</version> </dependency> <dependency> <groupId>org.

Spring Boot 3 Observability with Grafana

This article will teach you how to configure observability for your Spring Boot applications. We assume that observability is understood as the interconnection between metrics, logging, and distributed tracing. In the end, it should allow you to monitor the state of your system to detect errors and latency. There are some significant changes in the approach to observability between Spring Boot 2 and 3. Tracing will no longer be part of Spring Cloud through the Spring Cloud Sleuth project.

Kafka Transactions with Spring Boot

In this article, you will learn how to use Kafka transactions with the Spring Kafka project in your Spring Boot app. In order to run the Kafka cluster we will use Upstash. This article provides a basic introduction to Kafka transactions. If you are looking for more advanced usage and scenarios you may refer to that article, about distributed transactions in microservices. You can also read more about Kafka Streams the Spring Cloud Stream project in this article.