Java finally example

The code in finally block will always be executed except invoke System.exit() method. The example code is like following.

Spring data jpa example

This is a sample example to quick start to use spring data framework. I assume you have created a Mysql database which name is db. For quick start we only save data into database and query them out without any condition.

Book list

So many books, so little time.

Java force garbage collection

As we all know, JVM will do garbage collection when there is not enough memory to use. You can use System.gc() in your code to trigger garbage collection. But when JVM does this is not uncertainty.

spring boot handlerexceptionresolver example

There are many ways to handle the exception in Spring Boot. You can click Spring Boot exception handler example or Spring Boot global exception example to see how to handle exception in controller or handler. I will show you how to handle exception by implementing HandlerExceptionResolver.

Java add separator to string

Let's say you have a list of string and you want to add separator to them. If you use JDK8+ you can use String.join(). For JDK7-, I recommend you to use StringUtils in apache-commons-lang. The code is like following.

Java understand NoClassDefFoundError

Today I got NoClassDefFoundError when I test my new feature. The stack information is like following.

Spring Boot global exception example

I have shown you how to handle exceptions in controller. Click here for more detail. You can use @ControllerAdvice to define a global handler. This handler is for handling global exceptions. The structure of this example project is like following.

Spring Boot Thymeleaf example

Thymeleaf is a Java template engine. In this page I will show you how to use it in Spring Boot. You can use it to display the page. The structure of project is like following.

Spring Boot exception handler example

In this page I will show you how to use ExceptionHandler annotation handle exceptions. The structure of project is like following.

Linux combine files into one

We use cat command to combine multiple files into one. Here are the examples. Let's say you have 3 files like following.

Jackson convert json to pretty json

I have showed you how to convert object to pretty json in my previous blog. Today I will how you how to convert json string to pretty json. Before using writerWithDefaultPrettyPrinter method we need to convert json string to Object first then convert object to pretty json.

Java remove all null elements from list

If there are more than one null elements in list list.remove() method can not remove all of them. Use list.removeAll() to remove them. Example code is like following.

Could not locate executable winutils.exe

Today I got the error message like following.

Java visitor pattern example

The visitor is the most complex design pattern. This pattern shows us how to "visit" the elements of one structure without changing the code of element's. Add a new visitor when you want to add a new operation for these elements.