How to pretty print JSON using Moshi

This article shows how to use Moshi’s JsonAdaptor indent method to enable the pretty print JSON. Table of contents: 1. Download Moshi 2. Compact print JSON (Default) 3. Pretty print JSON using Moshi 4. Download Source Code 5. References P.S Tested with Moshi 1.15.1 1. Download Moshi Declare moshi in the pom.xml. pom.xml <dependency> <groupId>com.squareup.moshi</groupId> …

Read more

Moshi – java.math.BigDecimal requires explicit JsonAdapter to be registered

Moshi does not have built-in adapters to process types like java.math.BigDecimal; we need to manually provide a custom adapter to handle the BigDecimal type. Table of contents: 1. Download Moshi 2. BigDecimal requires explicit JsonAdapter 3. Make Moshi supports BigDecimal 3.1 Define BigDecimal Adapter 3.2 Register the Adapter with Moshi 4. Download Source Code 5. …

Read more

Jackson and Lombok examples

This article shows how to use Jackson and Lombok together. Table of contents: 1. Download Jackson and Lombok 2. Model with Lombok annotations 3. Parse JSON using Jackson 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 and Lombok 1.18.32 1. Download Jackson and Lombok Declares jackson-databind and lombok at pom.xml. pom.xml <dependency> …

Read more

Write JSON to a file with Jackson

This article shows you write JSON to a file with Jackson. Table of contents: 1. Download Jackson 2. Define a Java Object 3. Write JSON to a File 4. Download Source Code 5. References P.S Tested with Jackson 2.17.0 1. Download Jackson Declare jackson-databind in the pom.xml. pom.xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.0</version> </dependency> 2. Define …

Read more

How to ignore a field with Jackson

In Jackson, we can use @JsonIgnore to ignore a single field, @JsonIgnoreProperties to ignore multiple fields and @JsonIgnoreType to ignore a specified type during JSON serialization and deserialization. Table of contents: 1. Setup Jackson 2. Jackson @JsonIgnore – Ignores a field 3. Jackson @JsonIgnoreProperties – Ignores multiple fields 4. Jackson @JsonIgnoreType – Ignores a specified …

Read more

How to Run a CommandLineRunner Bean Conditionally in Spring Boot

This article shows different ways to run a CommandLineRunner bean conditionally in a Spring Boot application. Table of contents: 1. Using @ConditionalOnProperty 2. Using Environment 3. Using Spring Profiles 4. Check the Presence of Other Beans 4.1 Using @ConditionalOnBean 4.2 Using @ConditionalOnMissingBean 5. Download Source Code 6. References P.S. Tested with Spring Boot 3.1.2 1. …

Read more

Spring Boot CommandLineRunner Example

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. …

Read more

Jackson Java 8 date/time type `java.time.LocalDate` not supported by default

This article shows how Jackson can support the Java 8 date time APIs or JSR-310. Table of contents: 1. Download Jackson 2. Jackson does not support Java 8 date time APIs 3. Make Jackson support Java 8 date time APIs or JSR310 3.1 jackson-datatype-jsr310 3.1 Register JavaTimeModule() 4. Download Source Code 5. References P.S Tested …

Read more

Spring Boot @ServiceConnection Example

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 …

Read more

Spring Boot @DynamicPropertySource Example

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 …

Read more

How to restart Nginx on macOS

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 …

Read more

Spring Boot @ConditionalOnProperty Example

In Spring Boot, we can use the @ConditionalOnProperty annotation to conditionally register the beans based on the property value in the application.properties or application.yml file. This article will use the @ConditionalOnProperty annotation to simulate a toggle feature to turn features on or off at runtime without any code changes. Technologies used: Spring Boot 3.1.2 Java …

Read more

Spring Data JPA Paging and Sorting example

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 …

Read more

mysql-connector-java: unknown was not found

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 …

Read more

How to delete .DS_Store files on macOS

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 . – …

Read more

How to run an init script for Docker MariaDB

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 …

Read more

Docker – Running MariaDB as a container

This article shows how to use Docker to run MariaDB as a container. Tables of contents: 1. Run MariaDB as a container 2. Specify the MariaDB version 3. Access the running MariaDB container 4. MariaDB Persistent Storage with Volume 5. Access the MariaDB container log 6. Stop the MariaDB Container 7. Start the MariaDB Container …

Read more

Testing Spring Data JPA with @DataJpaTest

This article shows how to use @DataJpaTest to test the Spring Data JPA application. Technologies used: Spring Boot 3.1.2 Spring Data JPA (Hibernate 6 is the default JPA implementation) H2 in-memory database Maven Java 17 JUnit 5 Table of contents: 1. Project Directory 2. Spring Data JPA – Entity and Repository 3. @DataJpaTest 3.1 Disable …

Read more

How to run an init script for Docker Postgres

The official Docker postgres 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. Copy init.sql to container 3. Start the Postgres container 4. Access the Postgres container 5. References Technologies used: Docker 24.0.5 Official Docker postgres image (latest) 1. Create …

Read more

Spring Boot – package javax.persistence does not exist

We migrated the project to Spring Boot 3, and the JPA entity class hits package javax.persistence does not exist. java: package javax.persistence does not exist import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; P.S. Tested with Spring Boot 3.1.2 Solution Spring Boot 3 migrated all Java EE APIs javax.* to their equivalent Jakarta EE variant …

Read more

How to load custom properties files in Spring Boot

This article shows how to use @PropertySource annotation to load custom properties files in Spring Boot. Table of contents: 1. Load default properties file 2. Load custom properties files 3. Load multiple custom properties files 4. Download Source Code 5. References P.S. Tested with Spring Boot 3.1.2 1. Load default properties file Spring default loads …

Read more

How to generate Maven Wrapper files (mvnw and mvnw.cmd)

This article shows how to generate the Maven Wrapper files in a Maven project: mvnw (For Linux or macOS) mvnw.cmd (For Windows) .mvn/wrapper/maven-wrapper.jar .mvn/wrapper/maven-wrapper.properties Table of contents 1. What is Maven Wrapper 2. Generate the mvnw and mvnw.cmd 2.1 Ensure Maven installed 2.2 Navigate to the Maven project 2.3 Generate Maven Wrapper files 2.4 Build …

Read more

Spring Boot Testcontainers Example

This article shows how to test the Spring Boot REST endpoints using TestRestTemplate and Testcontainers (PostgreSQL container). Technologies used : Spring Boot 3.1.2 (Spring Web MVC, Spring Data JPA and Spring Test) Testcontainers 1.19.0 PostgreSQL 15, Alpine Linux base image postgres:15-alpine Java 17 JUnt 5 Tables of contents: 1. Project Structure 2. Project Dependencies 3. …

Read more

Spring @TestPropertySource example

In Spring integration tests, we can use @TestPropertySource to override the application properties which default loaded from application.properties or application.yml. Table of contents 1. Service Class 2. Test with the @TestPropertySource 3. @TestPropertySource Inline Properties 4. Download Source Code 5. References P.S Tested with Spring Boot 3.1.2 1. Service Class 1.1 We have a service …

Read more

How to access values from application.properties in Spring Boot

In Spring Boot, we can use @Value annotation to access the values defined in the application.properties from Spring-managed beans. application.properties #Logging logging.level.org.springframework.web=ERROR logging.level.com.mkyong=DEBUG email.smtp.server=smtp.gmail.com import org.springframework.beans.factory.annotation.Value; @Value("${email.smtp.server}") private String server; Table of contents: 1. application.properties 1. @Value 2. Environment 3. @ConfigurationProperties 4. Download Source Code 5. References P.S. Tested with Spring Boot 3.1.2 1. application.properties …

Read more

package javax.validation.constraints does not exist

Below is a Spring Boot application and validation bean API example. GlobalProperties.java import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; @Component @ConfigurationProperties @Validated public class GlobalProperties { //@Value("${thread-pool}") @Max(5) @Min(0) private int threadPool; Run the Spring Boot application and hits the following error: Terminal package javax.validation.constraints does not exist Solution – …

Read more

Spring Boot Test if web server starts on a specific port

Below is a Spring Boot Test example to test whether the web server started on port 8181. P.S. Tested with Spring Boot 3 and JUnit 5. application.properties server.port = 8181 WebServerTests.java package com.mkyong; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class WebServerTests { @LocalServerPort private int port; @Test public …

Read more

Cannot set LC_CTYPE to default locale: No such file or directory

When ssh into a production server, Debian, and noticed there is a warning or error as follows: Terminal manpath: can’t set the locale; make sure $LC_* and $LANG are correct mkyong@test-server:~$ Check the server’s locale; there are warning about the LC_CTYPE and LC_ALL variables. Terminal $ locale locale: Cannot set LC_CTYPE to default locale: No …

Read more

How to install MariaDB on Debian 11

This tutorial shows you how to install MariaDB on Debian 11. Table of contents: 1. Install MariaDB 2. Verify MariaDB 3. Secure the MariaDB 3.1 Enter current password for root (enter for none): enter 3.2 Switch to unix_socket authentication [Y/n] n 3.3 Remove anonymous users? [Y/n] y 3.4 Disallow root login remotely? [Y/n] y 3.5 …

Read more