Mkyong.com
1,023 FOLLOWERS
Mkyong.com is providing Java and Spring tutorials and code snippets since 2008. All published articles are simple and easy to understand and well tested in our development environment.
Mkyong.com
5M ago
This article tells what is CommandLineRunner and different ways of implementing it or use it in Spring Boot.
Table of contents:
1. What is CommandLineRunner?
2. Implementing CommandLineRunner
3. @Bean CommandLineRunner
4. Download Source Code
5. References
P.S. Tested with Spring Boot 3.1.2
1. What is CommandLineRunner?
The CommandLineRunner is an interface in Spring Boot. When a class implements this interface, Spring Boot will automatically run its run method after loading the application context. Usually, we use this CommandLineRunner to perform startup tasks like user or database initial ..read more
Mkyong.com
6M ago
Try Jackson to convert an object containing the Java 8 time APIs like LocalDate to a JSON formatted string and hits the errors Java 8 date/time typejava.time.LocalDatenot supported by default
Book.java
import java.math.BigDecimal;
import java.time.LocalDate;
public class Book {
private Long id;
private String title;
private BigDecimal price;
// Java 8 only
private LocalDate publishDate;
//...
}
App.java
ObjectMapper om = new ObjectMapper();
Book book =
new Book(1L, "Book A",
BigDecimal.valueOf(9.99),
LocalDate.of(2023, 10, 1 ..read more
Mkyong.com
6M ago
Spring Boot 3.1 introduced @ServiceConnection to enable the Spring Boot’s auto-configuration to use the service connection details to connect to a remote service (Testcontainers).
Tables of contents:
1. Testing with @DynamicPropertySource
2. Spring Boot 3.1 and @ServiceConnection
3. Download Source Code
4. References
1. Testing with @DynamicPropertySource
The following example uses @DynamicPropertySource to register the dynamic property values (data source connection details) to a PostgreSQL container.
BookRepositoryServiceConnectionTest.java
import org.springframework.test.context.DynamicP ..read more
Mkyong.com
6M ago
In Spring Boot, we can use @DynamicPropertySource to dynamically register or override property values in the ApplicationContext for integration tests.
Tables of contents:
1. Testing with ApplicationContextInitializer
2. Testing with @DynamicPropertySource
3. Testing with Spring Boot 3.1 and @ServiceConnection
4. Download Source Code
5. References
P.S. The @DynamicPropertySource feature is available from Spring Framework 5.2.5 and Spring Boot 2.3.1 onwards.
1. Testing with ApplicationContextInitializer
Before Spring 5.2.5, it was necessary to use @ContextConfiguration and ApplicationContextIn ..read more
Mkyong.com
6M ago
We open a Terminal and use the one-liner command sudo nginx -s stop && sudo nginx to restart the Nginx on macOS.
Restart the Nginx service.
Terminal
sudo nginx -s stop && sudo nginx
Reload the Nginx configuration file.
Terminal
sudo nginx -s reload
Table of Contents
1. Restart the Nginx on macOS
2. Reload the Nginx configuration file on macOS
3. Verify the Nginx Restart
4. References
P.S. Tested with macOS 13 and Nginx 1.21
1. Restart the Nginx on macOS
The Nginx has no official restart command; however, we can easily combine two commands to restart the Nginx process ..read more
Mkyong.com
6M ago
This article shows how to do Spring Data JPA paging and sorting using the PagingAndSortingRepository interface.
Technologies used:
Spring Boot 3.1.2
Spring Data JPA
H2 in-memory database
Java 17
Maven 3
JUnit 5
Spring Integration Tests with TestRestTemplate
Unit Tests with Mocking (Mockito)
Table of contents:
1. Extends PagingAndSortingRepository
2. Extends JpaRepository
3. Pageable and Sort examples
4. Spring Data JPA Paging and Sorting example
4.1 Project Directory
4.2 Project Dependencies
4.3 JPA Entity
4.4 Repository
4.5 Service
4.6 Controller
4.7 Start Spring Boot Application
5. Spr ..read more
Mkyong.com
6M ago
This article shows how to use the -U (uppercase U) option to change the user agent in wget command.
Terminal
wget -U {user-agent} {location}
Change user agent in wget
The below command changes the wget user agent to Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7).
Terminal
wget -U "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)" https://mkyong.com
Default user agent in wget
The default user agent for wget is Wget/VERSION.
Terminal
Wget/1.20.1
Note
Many websites have firewalls that are set, by default or through configuration, to block the default ‘wget’ user agent due to concerns ..read more
Mkyong.com
6M ago
The IntelliJ cannot find the mysql-connector-java dependency and hits error "unknown was not found"?
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Terminal
mysql:mysql-connector-java:jar:unknown was not found in https://repo.maven.apache.org/maven2
during a previous attempt.
This failure was cached in the local repository and resolution is not reattempted until the
update interval of central has elapsed or updates are forced
Solution
Repl ..read more
Mkyong.com
6M ago
This article shows how to delete .DS_Store files from the current directory and all its subdirectories on macOS.
1. Delete “.DS_Store” files
The below command will delete .DS_Store files from the current directory and all its subdirectories without asking for confirmation.
Terminal
find . -name ".DS_Store" -type f -delete
The command breakdown:
find . – Start searching in the current directory (. represents the current directory).
-name ".DS_Store" – Match files with the name ".DS_Store".
-type f – Only match files (not directories).
-delete – Delete all files that match the criteria.
I ..read more
Mkyong.com
6M ago
The official Docker mariadb image will run all *.sh and *.sql scripts in its /docker-entrypoint-initdb.d directory automatically when it starts.
Table of contents:
1. Create an init.sql
2. Run an init script for Docker MariaDB
3. Access the MariaDB running container
4. References
Technologies used:
Docker 24.0.5
Official Docker mariadb image (latest tag)
1. Create an init.sql
Creates an init.sql with the following content:
init.sql
CREATE DATABASE IF NOT EXISTS fruitdb;
USE fruitdb;
CREATE TABLE IF NOT EXISTS fruits (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL ..read more