Moises Macero's Blog, page 3

April 24, 2020

How Spring Boot auto-configuration works

In this post, we���ll create a basic Spring Boot application that includes the Web dependency. Then, we���ll analyze it to see how we got an embedded Tomcat web server in our application. This way, we���ll get to know how the magic, or better the auto-configuration, works in Spring Boot.

Table of Contents Creating a basic Spring Boot application The promise: an embedded Tomcat server Auto-configuration and the Tomcat embedded server Finding Tomcat The Spring Boot Autoconfigure module Web...
 •  0 comments  •  flag
Share on Twitter
Published on April 24, 2020 22:00

February 9, 2020

Shebang script files with Java and Docker

The implementation of the JEP 330: Launch Single-File Source-Code Programs available from Java 11 allows us to write a script in Java and run it as a shebang file. This is very useful for developers like me, who are not very familiar with scripting with other languages like bash or python.

In this post, I���ll show you how to write a single-file script in Java and run it from the command line. As an extra topic, you���ll learn how to put this file in a Docker image for even easier...

 •  0 comments  •  flag
Share on Twitter
Published on February 09, 2020 22:00

September 8, 2019

Custom Error Handling in REST Controllers with Spring Boot

This guide shows you how to implement custom error handling in Spring Boot. We use not only the well-known ControllerAdvice and ExceptionHandler annotations but also DefaultErrorAttributes and ErrorController to make your custom error attributes uniform and consistent.

Introduction

First, this guide covers the basic topics: how to capture an exception at controller level in Spring Boot and map it to a given HTTP status code. We���ll do that with the @ExceptionHandler and @ControllerAdvice...

 •  0 comments  •  flag
Share on Twitter
Published on September 08, 2019 22:33

January 6, 2019

Java 12's Switch Expressions in 5 minutes

The version 12 of Java comes with a new preview feature (amongst others): Switch Expressions. Let���s see how this feature can simplify some of our daily tasks.








[image error]



Java 12 Switch Expressions





Table of Contents


Java 12���s Simplified Switch Block
Classic switch block in Java
The new arrow syntax in switch blocks


Java 12���s Switch Expressions
Running the example code
Gettin...
 •  0 comments  •  flag
Share on Twitter
Published on January 06, 2019 22:34

Java 12���s Switch Expressions in 5 minutes

The version 12 of Java comes with a new preview feature (amongst others): Switch Expressions. Let���s see how this feature can simplify some of our daily tasks.

[image error]

Java 12���s Simplified Switch Block Classic switch block in Java

Before the release of this new preview feature, the only way to write a switch block in Java implies the usage of case:��(with a colon):

int numDays; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9:...
 •  0 comments  •  flag
Share on Twitter
Published on January 06, 2019 22:34

November 24, 2018

Spring Boot and Kafka ��� Practical Example

This blog post shows you how to configure Spring Kafka and Spring Boot to send messages using JSON and receive them in multiple formats: JSON, plain Strings or byte arrays.



This sample application also demonstrates how to use multiple Kafka consumers within the same consumer group with the @KafkaListener annotation, so the messages are load-balanced. Each consumer implements a different deserialization approach.



Besides, at the end of this post, you will find some practical exercises in case y...

 •  0 comments  •  flag
Share on Twitter
Published on November 24, 2018 01:40

Spring Boot and Kafka ��� Practical Configuration Examples

This blog post shows how to configure Spring Kafka and Spring Boot to send messages using JSON and receive them in multiple formats: JSON, plain Strings or byte arrays. Based on this configuration, you could also switch your Kafka producer from sending JSON to other serialization methods.

This sample application also demonstrates the usage of three Kafka consumers within the same consumer group, so the messages are load-balanced between the three. Each consumer implements a different...

 •  0 comments  •  flag
Share on Twitter
Published on November 24, 2018 01:40

August 6, 2018

How to deploy a Spring Boot WAR to Wildfly, JBoss or Tomcat

This is a short guide on how to deploy a Spring boot war packaged application to Wildfly and Tomcat. As you know, Wildfly is the name of the community version of JBoss EAP, so this configuration should work for JBoss EAP as well.

Introduction Updated: Code examples use Wildfly 18, Java 11 and Spring Boot 2.2. More info about code examples.

Spring Boot guides encourage you to deploy your application with the embedded server when you package the jar file. The default server depends now on...

 •  0 comments  •  flag
Share on Twitter
Published on August 06, 2018 11:05

July 30, 2018

Java and JSON ��� Jackson Serialization with ObjectMapper

This guide contains examples that show you how to serialize and deserialize from Java to JSON. It explains how to work with Strings, Objects, LocalDate, Optional, Lists, Maps, etc.

The code blocks are just test cases so you can verify the output and play with the examples. Note that, in real life, you shouldn't create the ObjectMapper��each time you need to parse or generate JSON. It's much more efficient to reuse it once you have configured it. This is, for instance, what you do in Spring...

 •  0 comments  •  flag
Share on Twitter
Published on July 30, 2018 22:38

May 10, 2018

Write BDD Unit Tests with BDDMockito and AssertJ

In this post, I���ll show you how to write more readable Unit Tests using a Behavior-Driven Development style (BDD). This is a coding style that is very easy to adopt and, at the same time, brings a huge benefit: it increases your test readability (a lot). Besides, it���s a small change that may drive you to go full-BDD (i.e. writing your��test scenarios before your code).

With a practical example, you���ll see how to make this change with two very popular libraries: Mockito (and its...

 •  0 comments  •  flag
Share on Twitter
Published on May 10, 2018 07:47