Quickly Developing Spring Boot Applications with Spring Boot CLI

Spring Boot CLI Introduction The Spring Boot CLI is a command line tool (CLI Tool) that you can use to help you quickly develop a Spring application if you want to do so. You just need to write a Groovy script first, and then you can immediately run a Spring Boot application through the Spring Boot CLI. Of course, you can also use the Spring Boot CLI to quickly build a Spring Boot project based on Maven or Gradle (via the Spring Initializr service).

How to Deploy Spring Boot Applications to WildFly Application Server

The open source WildFly Application Server (WildFly AS) is an upstream project of the commercial JBoss EAP (Enterprise Application Server). This means that JBoss EAP is based on WildFly AS, and you will probably know how to deploy to JBoss EAP once you learn how to deploy applications to WildFly AS. In today’s article, I’m going to introduce how to deploy a Spring Boot application to the WildFly application server.

Spring MVC ViewResolver

ViewResolver Overview The role of ViewResolver component is to resolve the corresponding view View object based on the view name and the localized Locale. In the SpringMvc source code, ViewResolver is an interface. This interface defines one method. View resolveViewName(String viewName, Locale locale) The interface method takes two parameters viewName and locale, viewName is the view name and locale is the localization parameter. In general, we only need the viewName to find the corresponding view.

Unit and Integration Testing in Spring Boot Micro Service

Let’s write tests using Mockito and JUnit Hello guys!!! How is your day? I thought of focusing on testing side this time. If you are a good developer, you should be able unit test the functionalities you are implementing. Then developer also can assure that the feature is working fine in a technical manner. This is an additional advantage that we get. So, I’m trying to show you how to write simple unit tests and integration tests for a Spring Boot application.

SpringSecurity-new objects can also be managed by the Spring container

It is reasonable to say that the new objects and the container are not related, but in the Spring Security framework also new a lot of objects out, but also can be managed by the container. How is this done? In the Spring Security authorization configuration code of some projects, you will find a line of code like this. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.

Gracefully record the payload of an HTTP request or response

Often encounter the need to handle http requests and response body scenarios. One of the bigger problems here is that once the requestBody or responseBody stream of a servlet has been read, it cannot be read again. For this problem, Spring itself provides a solution, namely. ContentCachingRequestWrapper ContentCachingResponseWrapper Write a filter. 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 public abstract class HttpBodyRecorderFilter extends OncePerRequestFilter { private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 1024 * 512; private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { boolean isFirstRequest = !

Using StopWatch elegantly outputs the time spent on execution

Background Sometimes we need to record the execution time of each task when doing development, or record the execution time of a piece of code, the simplest way is to print the difference between the current time and the execution time, and then if the implementation of a large number of tests is very troublesome, and not intuitive, if you want to do further control of the execution time, it is necessary to modify many places in the program.

Creating a custom Spring Cloud Gateway Filter

In this article, we look into writing a custom extension for Spring Cloud Gateway. Before we get started, let’s go over how Spring Cloud Gateway works: First, a client makes a network request to the Gateway The Gateway is defined with a number of routes, each with Predicates to match the request to the route. For example, you can match on the path segment of the URL or the HTTP method of the request.

Apache HttpClient 5 tutorial

Hypertext Transfer Protocol (HTTP) is probably one of the most important protocols on the Internet today. Web services, microservices, and services on various devices that support the Web are almost all HTTP, and the HTTP protocol has moved from Web browsers to a wider range of usage scenarios. Although the java.net package already provides the basic functionality for HTTP access to resources, it is not flexible enough and cannot be

Introducing Experimental Spring Support for Apache Pulsar

We are happy to announce that we are incubating a new experimental Spring project for Apache Pulsar. This project aims to provide Spring-friendly APIs, building blocks, and programming models for writing Java applications that interact with Apache Pulsar. Apache Pulsar is a popular messaging system with a growing ecosystem of developers in the enterprise messaging and streaming space. Here are some main features and advantages of using Apache Pulsar for messaging-based software applications:

Quickly bootstrap a new Spring Boot project with the Spring CLI

Generating a Spring Boot project can be made through various means, each having its own use case depending on your situation. You can use the Spring Initializr web UI which is beginner-friendly and perfect to get an overview of what is available. You can also generate your project inside your favorite IDE like Eclipse or Intellij by using plugins. And finally, you can generate your project without leaving your shell with the Spring CLI, and that is what we are going to do.

HandlerMapping in Spring MVC

HandlerMapping Overview The HandlerMapping component parses a Request and finds a Handler that handles the Request, which is generally understood as a method in the Controller. The HandlerMapping component does two main events. when the component is initialized, it registers the Request request and the corresponding Handler, which actually means that the Request and the corresponding Handler exist in a map as a key-value pair. parse each Request request and find the corresponding handler in the registered map.

Using @Async in a circular dependency will throw a BeanCurrentlyInCreationException

Background Today I found that a colleague’s spring boot project failed to start, and the exception log output from the console is as follows. 1 2 3 4 5 Exception in thread "main" org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'AService': Bean with name 'AService' has been injected into other beans [BService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean.

SpringBoot Unit Testing

Throughout the software delivery process, the unit testing phase is one of the earliest to find problems, and can be repeated back to the problematic phase, the more adequate the testing done in the unit testing phase, the more the software quality can be guaranteed. Refer to the sample project for the specific code. 1. Overview The full-link testing of a function often depends on many external components, such as database, redis, kafka, third-party interfaces, etc.

A Deep Dive Into Spring Boot Configuration

In this article, you will learn more about Spring Boot configuration. I’ll show you how to use it effectively in different environments. Especially, we talk a little bit more about configuration for Kubernetes. There are a lot of available options including properties, YAML files, environment variables, and command-line arguments. The thing we want to achieve is a strict separation of config from code for our app. We should comply with the third rule of the Twelve-Factor App.

ActiveMQ Artemis with Spring Boot on Kubernetes

This article will teach you how to run ActiveMQ on Kubernetes and integrate it with your app through Spring Boot. We will deploy a clustered ActiveMQ broker using a dedicated operator. Then we are going to build and run two Spring Boot apps. The first of them is running in multiple instances and receiving messages from the queue, while the second is sending messages to that queue. In order to test the ActiveMQ cluster, we will use Kind.

Smooth upgrade of Spring Cloud services in K8S

Background Currently, the company’s services are deployed using Spring Cloud framework and k8s, but when new services need to be upgraded, although the current rolling update method is used, there will be a vacuum time ranging from 30 seconds to 1 minute when the services are registered to Eureka, which will cause the online services to be inaccessible for a short period of time, so In the service upgrade, it

Stop using Field injection in Spring applications

Spring Dependency Injection Let’s take a look at a few of Spring’s dependency injection methods: constructor injection, Field injection and Setter injection. Constructor injection Injects the dependent object into the dependency via the constructor parameters and at the time of initializing the object. Spring’s recommended injection method for mandatory dependency usage. 1 2 3 4 5 6 7 8 9 private DependencyA dependencyA; private DependencyB dependencyB;; // @Autowired is not required, a new feature was added in Spring 4.

Spring Cloud on GitHub Actions

GitHub Actions Overview GitHub Actions is a continuous integration service launched by GitHub. Workflows can be created to build and test each pull request for a repository, or to deploy merged pull requests to a production environment. GitHub Actions goes beyond DevOps and allows you to run workflows when other events occur in the repository. For example, you can run workflows to automatically add the appropriate tags when someone creates a new issue in your repository.

16 Best Practices for Spring Boot in Production

Spring Boot is the most popular Java framework for developing micro services. In this article, I’ll share with you the best practices I’ve adopted since 2016 for using Spring Boot in professional development. These are based on my personal experience and articles by some well-known Spring Boot experts. In this article, I’ll focus on Spring Boot-specific practices (and most of the time, also apply to Spring projects). The best practices are listed below, in no particular order.