This short guide will focus on a single specific aspect of custom bean validation. If you need to catch up on how to write a custom bean validator, check out the tutorial on reflectoring.io. What is usually missing from these how-tos is the handling of validators for an entire class instead of just a field and how to set custom errors for specific field errors in a class.
Why would you want to write a validator for an entire class?
You may run into a situation where the value of one field of a class depends on the value of another field. For example, the field “type” value impacts which values are valid for the field “content”.
But when you define a custom validator, the validation annotation @interface only represents a single error message. The result is that any field error would result in the same error message. In a web service, this is not very helpful for users of your API.
Almost all of the tutorials and blog posts I found on this topic were focused on Spring Boot because, starting with version 2, it uses Micrometer as its metrics framework. However, in a particular project at work we do not have access to Spring Boot let alone a recent Spring version. Therefore, I’m explaining how to include Micrometer in your non-Boot Spring application using XML configuration.
In this tutorial I will be using Spring 5 and Java 11, so not exactly the versions I’m dealing with at work, but the concepts are the same and everything can probably be copied exactly as shown here.
I tend to write a lot of background to paint a picture why I’m doing things, so I’ll try to keep it short for to move on to the code quickly.
I have a digital music collection that was sorted by the first letter of the artist (A, B, C etc.) and then the artist and underneath that the albums. While that is good to find things, it’s not optimal for listening in my car (via USB stick). Sometimes I find myself wanting to listen to all of Melodic Death Metal on shuffle play. My car doesn’t support this like iTunes, with its internal music library, which is why I wanted to group artists and albums by genre. Since I didn’t plan to do this all manually, I opted to write some scripts in Python.
The code is available on GitHub. If you have suggestions for improvements, please comment or create a pull request. I’m not a Python pro, so I’m sure there’s some room to make it better.
In my search for information about what a web.xml exactly is and does, I ran across a video series on YouTube of Dr. Jules White who created over 70 videos explaining the basics and advanced topics of creating web services for mobile applications. The videos are roughly between 5 and 15 minutes long, so they are ideal for in-between watching, without sacrificing in content. You can binge them too, of course.
What I found most pleasing is that his presentation style is very informative and professional. There are no awkward pauses or anything else that would make me cringe. It’s very pleasant to watch and there’s a lot of good information in it, even for someone that already has a background in building web applications.
I created this list of links to all the individual videos because wanted to have more structure and information than a YouTube Playlist can provide in case I want to go back and watch something particular. Additionally, there’s a little sorting and numbering bug in the YouTube Playlist 😉