### Add Unit API Springdoc Starter (Maven)
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Include the springdoc starter dependency for the unit-api in your Maven pom.xml file. This enables enhanced Swagger documentation for your API endpoints, providing examples for quantity properties.
```xml
com.raynigon.unit-apispringdoc-starter{{latest_version}}
```
--------------------------------
### JSON Input Examples for Quantity Serialization
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/jackson.md
Demonstrates various JSON formats that Jackson will accept for a quantity field annotated with @JsonUnit. These examples show how numbers, strings, and objects representing quantities are correctly parsed and converted to the specified unit (e.g., km/h).
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": 80
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "80"
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": {
"value:" 80,
"unit": "km/h"
}
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "80 km/h"
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "22.2222 m/s"
}
```
--------------------------------
### JSON Example: Number Input for Quantity
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Demonstrates accepting a numerical value for a quantity field in JSON, which will be interpreted according to the unit specified in the model's annotation (`@JsonUnit`).
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": 80
}
```
--------------------------------
### Add Unit API Springdoc Starter (Gradle)
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Include the springdoc starter dependency for the unit-api in your Gradle build file. This enables enhanced Swagger documentation for your API endpoints, providing examples for quantity properties.
```groovy
implementation("com.raynigon.unit-api:springdoc-starter:{{latest_version}}")
```
--------------------------------
### JSON Example: String Input for Quantity
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Demonstrates accepting a string representation of a numerical value for a quantity field in JSON. Jackson will parse this string into the appropriate quantity type.
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "80"
}
```
--------------------------------
### Add Spring Boot Unit-API Dependencies (Gradle)
Source: https://github.com/raynigon/unit-api/blob/main/docs/spring-boot/index.md
Includes the necessary Unit-API starters for Spring Boot using Gradle. These starters provide integration with Jackson for serialization/deserialization, JPA for database persistence, and Springdoc for API documentation.
```groovy
implementation("com.raynigon.unit-api:spring-boot-jackson-starter:{{latest_version}}")
implementation("com.raynigon.unit-api:spring-boot-jpa-starter:{{latest_version}}")
implementation("com.raynigon.unit-api:spring-boot-springdoc-starter:{{latest_version}}")
```
--------------------------------
### JSON Example: String Input with Unit (km/h)
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Demonstrates accepting a string value that includes the unit (km/h) for a quantity field in JSON. Jackson will parse this string and convert it if necessary to the target unit.
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "80 km/h"
}
```
--------------------------------
### Add Unit API Jackson and JPA Starters (Maven)
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Include the Jackson and JPA starter dependencies for the unit-api in your Maven pom.xml file. These starters enable automatic unit conversion for JSON serialization/deserialization and persistence, respectively.
```xml
com.raynigon.unit-apijackson-starter{{latest_version}}com.raynigon.unit-apijpa-starter{{latest_version}}
```
--------------------------------
### JSON Example: String Input with Unit (m/s)
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Demonstrates accepting a string value with a different unit (m/s) for a quantity field in JSON. Jackson will parse this string and convert it to the target unit (`km/h` in this case).
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "22.2222 m/s"
}
```
--------------------------------
### Add Spring Boot Unit-API Dependencies (Maven)
Source: https://github.com/raynigon/unit-api/blob/main/docs/spring-boot/index.md
Adds the required Unit-API starters for Spring Boot to your Maven project. These dependencies facilitate seamless integration with Jackson, JPA, and Springdoc, enhancing unit handling capabilities.
```xml
com.raynigon.unit-apispring-boot-jackson-starter{{latest_version}}com.raynigon.unit-apispring-boot-jpa-starter{{latest_version}}com.raynigon.unit-apispring-boot-springdoc-starter{{latest_version}}
```
--------------------------------
### Add Jackson Module Dependency for unit-api
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/jackson.md
Include the unit-api Jackson module in your project to enable Jackson serialization and deserialization of quantity types. This dependency should be added to your build configuration (pom.xml for Maven, build.gradle for Gradle).
```groovy
implementation("com.raynigon.unit-api:jackson-module:{{latest_version}}")
```
```xml
com.raynigon.unit-apijackson-module{{latest_version}}
```
--------------------------------
### Register SystemOfUnits Service
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/custom-units.md
This is a plain text file used for registering a custom `SystemOfUnits` with the Java service mechanism. The path `META-INF/services/javax.measure.spi.SystemOfUnits` and the fully qualified class name are essential for discovery.
```plaintext
com.company.product.units.CustomSystemOfUnits
```
--------------------------------
### Add Unit API Jackson and JPA Starters (Gradle)
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/spring-boot.md
Include the Jackson and JPA starter dependencies for the unit-api in your Gradle build file. These starters enable automatic unit conversion for JSON serialization/deserialization and persistence, respectively.
```groovy
implementation("com.raynigon.unit-api:jackson-starter:{{latest_version}}")
implementation("com.raynigon.unit-api:jpa-starter:{{latest_version}}")
```
--------------------------------
### DummySystemOfUnits Implementation (Java/Groovy)
Source: https://github.com/raynigon/unit-api/blob/main/docs/java/index.md
Provides an example of a custom SystemOfUnits implementation. This is useful for extending the API with non-standard unit systems.
```Groovy
import com.raynigon.unit.api.core.units.SystemOfUnits
import com.raynigon.unit.api.core.units.UnknownUnit
import javax.measure.Unit
class DummySystemOfUnits implements SystemOfUnits {
override getUnits(): Set> {
setOf(UnknownUnit.INSTANCE)
}
override getUnit(String symbol): Unit<*>? {
null
}
}
```
--------------------------------
### DummyUnit Definition (Java/Groovy)
Source: https://github.com/raynigon/unit-api/blob/main/docs/java/index.md
An example of defining a custom Unit. This is a prerequisite for creating custom SystemOfUnits implementations.
```Groovy
import com.raynigon.unit.api.core.units.AbstractUnit
import javax.measure.Unit
class DummyUnit extends AbstractUnit {
// Dummy Unit - Does not represent any real physical quantity
private static final DummyUnit INSTANCE = new DummyUnit()
private DummyUnit() {
super("dummy", "dummy", Void::class.java)
}
@Override
protected Unit copy(String name, String symbol) {
this
}
@Override
protected Unit getSystemDefaultUnit() {
this
}
@Override
protected Unit getBaseUnits() {
this
}
@Override
protected Unit getConverterTo(Unit unit) {
this
}
@Override
protected Unit getConverterToAnyUnit(Unit> unit) {
this
}
static getInstance(): DummyUnit {
INSTANCE
}
}
```
--------------------------------
### Declare Model with @JsonUnit Annotation
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/jackson.md
Define your Java model classes using the @JsonUnit annotation to specify the target unit for quantity fields. This annotation guides Jackson on how to serialize and deserialize values, ensuring they are converted to the desired unit.
```java
public class BasicEntity {
public String id;
@JsonUnit(KilometrePerHour.class)
public Quantity speed;
}
```
--------------------------------
### Initialize Unit API Jackson with Basic Configuration
Source: https://github.com/raynigon/unit-api/blob/main/docs/java/jackson.md
This Groovy code snippet initializes the Unit API Jackson module with basic configuration. It's a minimal setup for integrating unit handling into Jackson's object mapping.
```groovy
new UnitApiJacksonModule()
```
--------------------------------
### Configure ObjectMapper with Unit API Features
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/modules/jackson-module.md
Java and Kotlin code examples demonstrating how to create an ObjectMapper instance and register the UnitApiModule with specific features enabled, such as SYSTEM_UNIT_ON_MISSING_ANNOTATION. This allows for customized handling of units during JSON processing.
```java
class JacksonConfiguration {
public ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(
UnitApiModule.withFeatures(UnitApiFeature.SYSTEM_UNIT_ON_MISSING_ANNOTATION)
);
return mapper;
}
}
```
```kotlin
class JacksonConfiguration {
fun createObjectMapper(): ObjectMapper {
val mapper = ObjectMapper()
mapper.registerModule(
UnitApiModule.withFeatures(UnitApiFeature.SYSTEM_UNIT_ON_MISSING_ANNOTATION)
)
return mapper
}
}
```
--------------------------------
### Define Custom SystemOfUnits in Kotlin
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/custom-units.md
This Kotlin code defines a custom `SystemOfUnits` named 'CUSTOM_UNIT'. It specifies how to retrieve units based on their symbol or quantity type. This system is intended to be registered with the default Java service mechanism.
```kotlin
package com.company.product.units
import javax.measure.spi.SystemOfUnits
import javax.measure.Unit
import javax.measure.UnitConverter
import javax.measure.quantity.Speed
import javax.measure.spi.quantity
import javax.measure.spi.Dimension
import com.raynigon.unit_api.core.units.IUnit
import com.raynigon.unit_api.core.units.TransformedUnit
import com.raynigon.unit_api.core.units.system.MetrePerSecond
import com.raynigon.unit_api.core.units.system.KilometrePerHour
import com.raynigon.unit_api.core.units.converter.MultiplyConverter
import com.raynigon.unit_api.core.units.Dimension
import java.util.Collections
class CustomSystemOfUnits : SystemOfUnits {
companion object {
const val ID = "CUSTOM_UNIT"
private val symbolToUnit: Map> = listOf(
KilometrePer100Hour()
).map { it.getSymbol() to it }.toMap()
}
override fun getName(): String {
return ID
}
override fun ?> getUnit(quantityType: Class): Unit? {
return units.filterIsInstance>().firstOrNull { quantityType.isAssignableFrom(it.quantityType) }
}
override fun getUnit(symbol: String): Unit<*>? {
return symbolToUnit[symbol]
}
override fun getUnits(): MutableSet> {
return symbolToUnit.values.toMutableSet()
}
override fun getUnits(dimension: Dimension?): MutableSet> {
return Collections.emptySet()
}
}
```
--------------------------------
### JSON Examples for Quantity Serialization
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/modules/jackson-module.md
Demonstrates various JSON formats that Jackson will accept for a 'speed' field annotated with @JsonUnit(KilometrePerHour.class). This includes plain numbers, strings, objects with value and unit, and strings with explicit units.
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": 80
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "80"
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": {
"value:" 80,
"unit": "km/h"
}
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "80 km/h"
}
```
```json
{
"id": "65bf1872-d197-4d72-9950-2b7b4d74a674",
"speed": "22.2222 m/s"
}
```
--------------------------------
### Define KilometrePer100Hour Unit in Kotlin
Source: https://github.com/raynigon/unit-api/blob/main/docs/_dep/quickstart/custom-units.md
This Kotlin code defines a custom `KilometrePer100Hour` unit, which represents speed. It inherits from `TransformedUnit` and is based on `KilometrePerHour` divided by 100. The `getSystemId` method links it to the `CustomSystemOfUnits`.
```kotlin
class KilometrePer100Hour :
TransformedUnit(
"km/100h",
"KilometrePer100Hour",
KilometrePerHour().divide(100),
MetrePerSecond(),
MultiplyConverter.of(1.0)
),
IUnit {
override fun getSystemId(): String {
return CustomSystemOfUnits.ID
}
override fun getQuantityType(): Class {
return Speed::class.java
}
override fun isSystemUnit(): Boolean {
return false
}
}
@Suppress("FunctionName")
fun KilometrePer100Hour(value: Number): Quantity = quantity(value, KilometrePer100Hour())
```
--------------------------------
### Kotlin Data Class with @JsonUnit Annotation
Source: https://github.com/raynigon/unit-api/blob/main/docs/spring-boot/springdoc.md
Example of a Kotlin data class demonstrating the usage of the `@JsonUnit` annotation. This annotation is used to specify the unit for a `Quantity>` property, which will then be reflected in the generated API documentation.
```kotlin
data class TelemetryRecordRequest(
@JsonUnit(MilliSecond::class)
val rtt: Quantity