Spring Boot @MockBean Example

Spring boot @MockBean annotation used to add mocks to a Spring ApplicationContext. In this tutorial, we will learn the different ways to use this annotation.

Maven – Remove All Corrupt Jars

Learn to rebuild local repository by deleting all corrupt jars from repository and fix missing maven dependencies problem or force update a maven project.

How to Write Integration Test in Spring Boot

Learn to write integration tests for given Spring rest controller apis using @SpringBootTest and Junit 5. This technique can be applied to spring boot as well as spring mvc applications, both.

Integration Testing with @SpringBootTest and JUnit 5

The @SpringBootTest annotation in Spring Boot allows us to create integration tests by loading the entire application context. It acts as a bridge between the application configuration and the test framework so that tests can verify the behavior of your application in an environment similar to production.

Spring Boot 3 with JUnit 5

Learn to include JUnit 5 in a Spring boot application and write unit tests. Check out a demo of JUnit 5 tests for Spring Boot REST controller.

Parsing RSS Feed using Retrofit2

In this Retrofit2 tutorial, we will learn to parse RSS feeds in an Android application using Retrofit2 and the SimpleXmlConverterFactory.

Parsing a Sitemap using Retrofit2

Learn to parse a sitemap in an Android app using Retrofit2, which uses XML parsing with a simple XML converter dependency. Here’s a Retrofit sitemap reader example.

Retrofit 2 Service Generator

Though we can easily create Retrofit instances on demand for different client in place, having a dedicated factory method or service generator method is definitely desirable. In this service generator, we can have centralized place to add/modify the authentication, logging and error handling logic. 1. Retrofit service generator A service …

Logging in Retrofit 2

Learn to add logging support in Retrofit 2 using HttpLoggingInterceptor and OkHttpClient APIs. In Retrofit 2, all network operations are performed via OkHttp library. OkHttp provides HttpLoggingInterceptor which logs HTTP request and response data. An example to add HttpLoggingInterceptor to OkHttpClient. 1. Dependency 2. Create and add HttpLoggingInterceptor we can …

Guide to Retrofit.Builder API

Retrofit.Builder class uses the Builder API to allow defining the URL end point for the HTTP operations and finally build a new Retrofit instance.

Retrofit 2 Tutorial: Declarative REST Client for Android

In this Retrofit 2 tutorial, we will learn the basics of Retrofit and then we will create a declarative REST client for Android for executing HTTP requests against a REST API. 1. What is Retrofit? Retrofit was developed by Square and in its documentation, it is a type-safe REST client …

Gson JsonParser

Gson JsonParser is used to parse Json data into a parse tree of JsonElement and thus JsonObject, which can be used to get JSON values using their keys.

Gson – Exclude or Ignore Fields

Gson allows us to exclude or ignore the fields from Java classes using @Expose annotation, exclude fields with modifiers and custom exclusion strategies.

Gson JsonReader

Learn to work with Gson JsonReader class which is a pull-based streaming JSON parser. It helps in reading a JSON as a stream of tokens. 1. JsonReader Read More : Streaming parser of XML 2. Tokens In streaming mode, every JSON data is considered an individual token. When we use …

Gson Tutorial: Read and Write JSON with Examples

All modern applications generally fetch data from remote services (e.g. REST or SOAP) that are mostly either XML or JSON format. Gson helps applications in Java-JSON serialization and deserialization automatically as well as manually, if needed, using simple toJson() and fromJson() methods. 1. Maven Refer to the latest version of …

Gson – Serialize Null Values

The default behavior in Gson is that null fields are ignored. For example, if in Employee object, we do not specify the email (i.e. email is null) then the email will not be part of serialized JSON output. Gson ignores null fields because this behavior allows a more compact JSON …

Gson – Pretty Print JSON

By default, Gson prints the JSON in a compact format. It means there will not be any whitespace in between field names and their value, object fields, and objects within arrays in the JSON output etc. 1. GsonBuilder.setPrettyPrinting() To enable the Gson Pretty Print feature, we must configure the Gson …

Gson – Parse JSON Array to Java Array or List

Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists. 1. Parsing JSON Array as Root To parse JSON, with array as root, we …

Java UUID Generator Example

Learn what is UUID and it’s versions and variants. Learn to generate UUID in Java using UUID.randomUUID() API. Also learn to generate version 5 UUID in Java. 1. What is UUID? UUID (Universally Unique IDentifier), also known as GUID (Globally Unique IDentifier) is 128 bits long identifier that is unique …

Pairs in Java

Learn to work with key value pairs in Java using Pair classes e.g. javafx.util.Pair, ImmutablePair, MmutablePair (common langs) and io.vavr.Tuple2 class. Read More : Tuples in Java 1. Why we need pairs? A pair provide a convenient way of associating a simple key to value. In Java, maps are used …

Spring Boot Disable Startup Banner

To disable the spring boot banner logo from the console or log files, we can make changes in 3 possible ways i.e. programmatically, properties, or startup arguments. 1. Disable Banner Programmatically Set banner mode in the program using setBannerMode(Banner.Mode.OFF) method call which starts the application. 2. Disable Banner using Properties …

Spring Boot Annotations

Learn about some frequently used spring boot annotations as well as which work behind the scene. e.g. @SpringBootApplication and @EnableAutoConfiguration.

Parse a String to UTC Date Time

Learn to convert a string to date time instance classes e.g. ZonedDateTime or OffsetDateTime classes, using DateTimeFormatter class in Java. 1. Instant, OffsetDateTime and ZonedDateTime Classes In Java 8, OffsetDateTime and ZonedDateTime – both store an instant on the universal timeline to nanosecond precision. OffsetDateTime adds to the instant the …

Spring Boot Console Logging

Learn to use and configure logging to console in spring boot application. Also learn to configure and use console appenders available in logback and log4j2.

Spring Boot Logging with Lombok

Project lombok can also be used to configure logging in spring boot applications and thus removing the boilerplate code for getting the logger instance.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.