When I’m learning a new framework, I like to look at things that beginners don’t like to read or don’t understand. For example, when I was learning Angular, it was obvious that ng new would be enough to create a new project and start writing programs, but I would go deeper into the whole process of starting it. And when I was learning Spring Boot, even though Spring Initializr was really nice to use, and I could start developing applications by just selecting the packages, I wanted to understand what was going on behind the scenes of this amazing design, so I could understand the core principles of a framework.
Formatting json Date/LocalDateTime/LocalDate in Spring Boot
Spring Boot uses jackson by default to serialize, deserialize json data.
By default, Jackson serializes Date objects as timestamps. For LocalDateTime, LocalDate objects, jackson doesn’t do anything special, it just treats them as basic Java objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import java.util.HashMap; import java.util.Map; import com.
Getting started with your first spring boot application from scratch
Spring Boot has been gaining traction in the Java community for a few years now, but instead of reinventing the wheel, it has integrated a bunch of good, off-the-shelf packages and designed some simple programming frameworks with IoC and AOP to greatly simplify the complexity of development and reduce the number of tedious setup steps. Instead of using Spring Initializr to get you started, I’m going to completely hand-carve the Spring Boot application and dissect the entire development and startup process from scratch.
Map a JSON POST to Multiple Spring MVC Parameters
1. Overview When using Spring’s default support for JSON deserialization, we’re forced to map the incoming JSON to a single request handler parameter. Sometimes, however, we’d prefer a more fine-grained method signature.
In this tutorial, we will learn how to use a custom HandlerMethodArgumentResolver to deserialize a JSON POST into multiple strongly-typed parameters.
2. The Problem First, let’s look at the limitations of Spring MVC’s default approach to JSON deserialization.
Setup of spring boot application & initialization of PostgreSQL database on Kubernetes
Reference
Setup of spring boot application & initialization of PostgreSQL database on Kubernetes - PART 1 Setup of spring boot application & initialization of PostgreSQL database on Kubernetes - PART 2 How to deploy a containerized spring boot application , with PostgreSQL as database on minikube. This post will also share details on how to initialize the database with tables and data during the initialization process.
Will use a spring boot application order service with a REST endpoint to fetch customer details
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.