I'm experiencing some version confusion with Java Units of Measurement (UOM). I have a
import tec.uom.se.AbstractUnit;
public class Class0 {
/**
* Is an {@link AbstractUnit} in order to be serializable as {@code @Basic}
* with JPA.
*/
private AbstractUnit<?> unit;
public Class0() {
}
public Class0(AbstractUnit<?> unit) {
this.unit = unit;
}
public AbstractUnit<?> getUnit() {
return unit;
}
public void setUnit(AbstractUnit<?> unit) {
this.unit = unit;
}
}
which I'm serializing with
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opower.unitsofmeasure.UnitJacksonModule;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import tec.uom.se.AbstractUnit;
import static tec.uom.se.unit.Units.SQUARE_METRE;
public class TheTest {
@Test
public void test() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new UnitJacksonModule());
Class0 value = new Class0((AbstractUnit) SQUARE_METRE);
String json = objectMapper.writeValueAsString(value);
assertEquals("m²", json);
}
}
while using
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opower.unitsofmeasure</groupId>
<artifactId>jackson-module-unitsofmeasure</artifactId>
<version>1.3.5-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tec.uom</groupId>
<artifactId>uom-se</artifactId>
<version>1.0.9</version>
<type>jar</type>
</dependency>
That fails due to
java.lang.NoSuchFieldError: ONE
at systems.uom.ucum.UCUM.<clinit>(UCUM.java:114)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at tec.uom.se.format.SymbolMap.<init>(SymbolMap.java:115)
at tec.uom.se.format.SymbolMap.of(SymbolMap.java:142)
at systems.uom.ucum.format.UCUMFormat$Parsing.<clinit>(UCUMFormat.java:416)
at systems.uom.ucum.format.UCUMFormat.getInstance(UCUMFormat.java:106)
at com.opower.unitsofmeasure.UnitJacksonModule$UnitJsonSerializer.serialize(UnitJacksonModule.java:56)
at com.opower.unitsofmeasure.UnitJacksonModule$UnitJsonSerializer.serialize(UnitJacksonModule.java:42)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3893)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3207)
at de.richtercloud.jackson.unit.serialization.TheTest.test(TheTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
It's not obvious to my why this fails, I suspect a version or implementation conflict.
This is fixed by switching to
<dependency>
<groupId>tec.uom</groupId>
<artifactId>uom-se</artifactId>
<version>1.0.8</version>
</dependency>