Get JSON Content as Object Using MockMVC
Baeldung » Spring
by Ana Peterlić
2d ago
1. Overview When testing our REST endpoints, sometimes we want to obtain the response and convert it to an object for further checking and validation. As we know, one way to do this is by using libraries such as RestAssured to validate the response without converting it to an object. In this tutorial, we’ll explore several ways to get JSON content as an object using MockMVC and Spring Boot. 2. Example Setup Before we dive in, let’s create a simple REST endpoint we’ll use for testing. Let’s start with the dependency setup. We’ll add the spring-boot-starter-web dependency to our pom.xml so we ca ..read more
Visit website
How to Test a Spring AOP Aspect
Baeldung » Spring
by Manfred Ng
3d ago
1. Overview Aspect-oriented programming (AOP) improves program design by separating a cross-cutting concern into a base unit, called an aspect, from the main application logic. Spring AOP is a framework that helps us implement aspects with ease. AOP aspects are no different from other software components. They require different tests to verify their correctness. In this tutorial, we’ll learn how to conduct unit and integration tests on Spring AOP aspects. 2. What Is AOP? AOP is a programming paradigm that complements object-oriented programming (OOP) to modularize cross-cutting concerns, which ..read more
Visit website
Authenticate Using Social Login in Spring Authorization Server
Baeldung » Spring
by Andrei Branza
3d ago
1. Introduction In this tutorial, we’ll demonstrate how to setup the back end of a web application that uses Spring’s social login feature. We’ll use Spring Boot and the OAuth2.0 dependency. We’ll also use Google as the social login provider. 2. Register With Social Login Provider 2.1. Consent Screen Configuration Before we start the project setup, we need to obtain the ID and secret from the social login provider. Considering we’re using Google as the provider, let’s go to their api console to start the process. Once we’ve reached the Google API console, we need to create a new project. Once ..read more
Visit website
Setting Default TimeZone in Spring Boot Application
Baeldung » Spring
by Stelios Anastasakis
3d ago
1. Overview Sometimes, we want to be able to specify the TimeZone used by an application. For a service running globally, this could mean that all servers are posting events using the same TimeZone, no matter their location. We can achieve this in a few different ways. One approach involves the use of JVM arguments when we execute the application. The other approach is to make the change programmatically in our code at various points of the bootup lifecycle. In this short tutorial, we’ll examine a few ways to set the default TimeZone of a Spring Boot Application. First, we’ll see how to achiev ..read more
Visit website
How to Intercept a Request and Add Headers in WebFlux
Baeldung » Spring
by Ruchira Madhushan Rajapaksha
3w ago
1. Overview Interceptors, also known as filters, are a feature in Spring that allows us to intercept client requests. This allows us to examine and transform the request before the controller handles it or returns response to the client. In this tutorial, we’ll discuss various ways of intercepting a client request and adding custom headers using the WebFlux Framework. We’ll first explore how to do it for a specific endpoint. Then, we’ll determine the approach for intercepting all incoming requests. 2. Maven Dependency We’ll be using the following spring-boot-starter-webflux Maven dependency fo ..read more
Visit website
Get the Response Body in Spring Boot Filter
Baeldung » Spring
by Harpal Singh
3w ago
1. Introduction In this article, we’ll explore how to retrieve the response body from a ServletResponse in a Spring Boot filter. In essence, we’ll define the problem, and then we’ll use a solution that caches the response body to make it available in the Spring Boot filter. Let’s begin. 2. Understanding the Problem First, let’s understand the problem we are trying to solve. When working with Spring Boot filters, it is tricky to access the response body from the ServletResponse. This is because the response body is not readily available, as it is written to the output stream after the filt ..read more
Visit website
The Difference Between Throwing an Exception and Mono.error() in Spring Webflux
Baeldung » Spring
by Reza Ganji
3w ago
1. Overview Error handling is a critical aspect of reactive programming with Spring WebFlux. Developers usually rely on two primary methods for error handling: throwing exceptions or using the Mono.error() method provided by Project Reactor. Both approaches are used to signal errors, but they have distinct characteristics and use cases. In this tutorial, we will explain the differences between throwing an exception and Mono.error() in Spring WebFlux. We’ll provide illustrative Java code examples to make it more understandable. 2. Traditional Approach: Throwing Exceptions For many years, throwi ..read more
Visit website
Disable @Cacheable in Spring Boot
Baeldung » Spring
by Suraj Mishra
1M ago
1. Introduction Caching is an effective strategy to improve performance by avoiding repeated execution of logic when the execution result hasn’t changed (effectively the same) for a known period. Spring Boot provides the @Cacheable annotation, which we define over a method, and it caches the result of the method. In some scenarios, such as during testing in a lower environment, we may need to disable caching to observe certain modified behaviors. In this article, we’ll configure the cache in Spring Boot and learn how to disable the cache when needed. 2. Caching Setup Let’s set up a simple use ..read more
Visit website
Disable @EnableScheduling on Spring Tests
Baeldung » Spring
by Adrian Chlebosz
1M ago
1. Introduction In this tutorial, we’ll dive into the topic of testing Spring applications which utilize scheduled tasks. Their extensive usage might cause a headache when we try to develop tests, especially integration ones. We’ll talk about possible options to make sure that they’re as stable as they can be. 2. Example Let’s start with a short explanation of the example we’ll be using throughout the article. Let’s imagine a system that allows the representative of the company to send notifications to their clients. Some of them are time-sensitive and should be delivered immediately, but some ..read more
Visit website
Logging in Spring Boot With Loki
Baeldung » Spring
by Suraj Mishra
1M ago
1. Introduction Grafana Labs developed Loki, an open-source log aggregation system inspired by Prometheus. Its purpose is to store and index log data, facilitating efficient querying and analysis of logs generated by diverse applications and systems. In this article, we’ll set up logging with Grafana Loki for a Spring Boot application. Loki will collect and aggregate the application logs, and Grafana will display them. 2. Running Loki and Grafana Services We’ll first spin up Loki and Grafana services so that we can collect and observe logs. Docker containers will help us to more easily configu ..read more
Visit website

Follow Baeldung » Spring on FeedSpot

Continue with Google
Continue with Apple
OR