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.

Usage and principle of BCryptPasswordEncoder

There is a tool class BCryptPasswordEncoder for password encryption in Spring Security, which is very simple and interesting to use. Let’s see how it works. Usage of BCryptPasswordEncoder First create a Spring Boot project and add the Spring Security dependency. Then create a test class with the following code. 1 2 3 4 5 6 7 8 9 10 11 12 13 final private String password = "123456"; @Test public void TestCrypt() { BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); String encode1 = bCryptPasswordEncoder.

Using JPDA to Remotely Debug SpringBoot Applications

Remote debugging techniques, which are relatively common in development. If the technical team is using DevOps-based development process, then you may need to understand remote debugging techniques. In our company, back-end development engineers, hardly ever start services locally for debugging. In some cases, unit tests and interface tests are not good enough to recover and track the weird bugs, and we have 2 internal environments (development and testing environments), which are developed locally, then the code is pushed to the “development environment” server and deployed automatically via CI/CD, and then debugged based on the development environment.

Spring Boot Hardening - Disclosed Version

The default configuration of Spring Boot tells quite a lot about errors and software versions used. This is a potential security leak and therefor should be avoided. One step is to get rid of server information from header and default error pages. Result With my suggested changes in place the default error result pages will look like this: 1 2 3 4 5 6 7 8 ➜ ~ curl -i http://127.

How to integrate Hibernates Multitenant feature with Spring Data JPA in a Spring Boot application

For quite some time now, Hibernate has offered a Multitenant feature. It integrates nicely with Spring, but there is not much information about how to actually set it up, so I thought an example or two or three could help. There is already an excellent blog article, but it is a little dated and it covers a lot of specifics to the business problems the author tried to solve. This approach hides a little of the actual integration, which will be the focus of this article.

Spring Cyclic Dependencies

Today, there are still many people arguing about cyclic dependencies, and many interviewers asking questions about cyclic dependencies, and even asking only about cyclic dependencies in Spring, which seems to be a must learn point in Spring, a feature that many people are talking about. In my opinion, this is a bit of a stain on the many good designs in the Spring framework, a compromise for bad design, you know, there are no circular dependencies in the whole Spring project, is this because the Spring project is too simple?

Spring Authorization Server Is Going 1.0

We are excited to announce that we’ve started preparing for Spring Authorization Server 1.0 with plans to release the GA version in November 2022. It has been just over two years since we initially announced this new project, and we have come a long way since its initial development. The project has a full feature set, and the APIs have stabilized and matured over this time. A lot of effort and care was put into this project to ensure that it can grow and adapt over the next few years.

Spring Boot application startup exception: java.lang.reflect.InaccessibleObjectException

The following error was encountered when the Spring Boot application was started: 1 2 3 4 5 6 7 8 java.lang.reflect.InaccessibleObjectException: Unable to make private native ... accessible。 Set com.sun.jndi.rmi.object.trustURLCodebase = false java.lang.reflect.InaccessibleObjectException: Unable to make private native java.lang.reflect.Field[] java.lang.Class.getDeclaredFields0(boolean) accessible: module java.base does not "opens java.lang" to unnamed module @326de728 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) The reason for the error is that the JVM’s java.

How to override a bean that has already been initialized

First, through the configuration class, create a bean of type java.util.Date. 1 2 3 4 5 6 7 8 @Configuration public class DateConfig { @Bean("date") public Date date(){ return new Date(); } } Time is not constant, I want to get the current time object in the form of a bean, how should I override the bean already in the container? At first I thought of using org.

Spring Reactive Read Flux Into Inputstream

1. Overview In this tutorial, we’ll deep dive into Java reactive programming to solve an interesting problem of how to read Flux<DataBuffer>into a single InputStream. 2. Request Setup As a first step to solving the problem of reading Flux<DataBuffer> into a single InputStream, we’ll use the Spring reactive WebClient for making a GET request. Further, we can use one of the public API endpoints hosted by gorest.co.in for such testing scenarios:

Implement Role-based authorization in Spring Boot with Keycloak

Role-based access control is a must-have for any application dealing with users who can access resources depending on their organization’s role. In a previous article, we have learned how to secure our Spring Boot REST API with Keycloak by using the OpenID Connect authentication protocol. In this article, we are going to build on this sample application and add role-based authorization. The purpose is to authorize access of some endpoints only to users having a specific role.

Spring ShedLock Guide

Spring Shedlock is used to perform timed tasks in the case of distributed services, such as regularly deleting some data in the database, doing data migration and other operations. This technique is heavily used in the distributed services of the project. The main reasons for its use are the following. Timed tasks. To perform some operations behind the scenes during the normal operation of the service and to meet our business needs, a scheduled task is essential.

Apply Default Global SecurityScheme in springdoc-openapi

1. Overview In this tutorial, we’ll learn how to configure a default global security scheme and apply it as the default security requirement of the API using the springdoc-openapi library in a Spring MVC web application. Further, we’ll discuss how we can override these default security requirements. The OpenAPI specification lets us define a set of security schemes for the API. We can configure the security requirements of the API globally or apply/remove them per endpoint.

Spring Security - The request was rejected

Background of the problem When a user accesses a specific connection (e.g. http://localhost/index) without permissions, they are redirected to the login page http://localhost/login. In order to redirect to the target access page http://localhost/index after a successful login, Spring Security stores a message in the cookie, marked as a jsessionid. When redirected the Servlet container, i.e. tomcat or something like that will add the jsessionid to the back of the redirected url.