JSR-363 Units of Measurement API in Practice – Blogpost Series Introduction

By Johannes Rudolph27. October 2017

Many applications today deal with measurement data based on physical quantities. The Internet of Things (IoT) has brought us connected sensors that measure things like temperature or force and live-transmit them to applications in the cloud. Applications in the cloud again produce tons of metrics, for example about the usage of compute resources like Gigabytes of Storage used or the use of network bandwidth.

History has repeatedly demonstrated that dealing with units is a dangerous source of bugs, sometimes with disastrous consequences: rockets exploding, Missions to Mars failed. To avoid these bugs, applications need not only a concept of the numerical value of a measurement (e.g. a double or long) but also of its units of measurement. There are various libraries and sometimes even language features that can help us build applications that are able to accurately deal with data based on physical quantities and help ensure correctness of calculations like unit conversions.

We will look at the JSR-363 units of measurement API for the Java ecosystem. There\’s already a great user guide on how to use the core features of this library like the Unit and Quantity<q> types. So instead, I will show you how we solved some practical problems when using this library in a Spring-Boot application written in Kotlin for meshcloud’s cloud monitoring and billing systems.

We will cover these topics:

  1. Persisting Quantities with Spring Data
  2. Supporting Binary and SI Prefixes
  3. Supporting JSON Serialization for REST APIs