TL;
- When the
localName property of @JacksonXmlProperty is not set, the required of @JsonProperty does not detect the property name of POJO.
- I have an example to verify this phenomenon. I don't seem to see this documented in the documentation. This unit testing and reproduction process is located at https://github.com/linghengqian/jackson-mixed-annotations-test .
public class RequiredTest {
@Test
void assertRequiredValue() {
ObjectMapper XML_MAPPER = XmlMapper.builder().build();
Assertions.assertDoesNotThrow(() -> {
XML_MAPPER.readValue("""
<Record type="">
<driverClassName>
</driverClassName>
</Record>
""", TestRecord.class);
});
Assertions.assertDoesNotThrow(() -> {
XML_MAPPER.readValue("""
<Record type="">
<driverClassName>
</driverClassName>
</Record>
""", TestRecordWithoutLocalName.class);
});
}
@Getter
@NoArgsConstructor
@JacksonXmlRootElement(localName = "Record")
static class TestRecord {
private String type;
private String driverClassName;
@JsonCreator
public TestRecord(
@JsonProperty(required = true) @JacksonXmlProperty(localName = "type", isAttribute = true)
String type,
@JsonProperty(required = true) @JacksonXmlProperty(localName = "driverClassName")
String driverClassName) {
this.type = type;
this.driverClassName = driverClassName;
}
}
@Getter
@NoArgsConstructor
@JacksonXmlRootElement(localName = "Record")
static class TestRecordWithoutLocalName {
private String type;
private String driverClassName;
@JsonCreator
public TestRecordWithoutLocalName(
@JsonProperty(required = true) @JacksonXmlProperty(isAttribute = true)
String type,
@JsonProperty(required = true) @JacksonXmlProperty(localName = "driverClassName")
String driverClassName) {
this.type = type;
this.driverClassName = driverClassName;
}
}
}
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.273 s <<< FAILURE! -- in com.lingh.RequiredTest
[ERROR] com.lingh.RequiredTest.assertRequiredValue -- Time elapsed: 0.243 s <<< FAILURE!
org.opentest4j.AssertionFailedError:
Unexpected exception thrown: com.fasterxml.jackson.databind.exc.MismatchedInputException: Missing required creator property '' (index 0)
at [Source: (StringReader); line: 4, column: 1] (through reference chain: com.lingh.RequiredTest$TestRecordWithoutLocalName[""])
at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:152)
at org.junit.jupiter.api.AssertDoesNotThrow.createAssertionFailedError(AssertDoesNotThrow.java:84)
at org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:53)
at org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:36)
at org.junit.jupiter.api.Assertions.assertDoesNotThrow(Assertions.java:3168)
at com.lingh.RequiredTest.assertRequiredValue(RequiredTest.java:26)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Missing required creator property '' (index 0)
at [Source: (StringReader); line: 4, column: 1] (through reference chain: com.lingh.RequiredTest$TestRecordWithoutLocalName[""])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1781)
at com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer._findMissing(PropertyValueBuffer.java:192)
at com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer.getParameters(PropertyValueBuffer.java:158)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:301)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:202)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:526)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1493)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
at com.fasterxml.jackson.dataformat.xml.deser.XmlDeserializationContext.readRootValue(XmlDeserializationContext.java:104)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4899)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3846)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3814)
at com.lingh.RequiredTest.lambda$assertRequiredValue$1(RequiredTest.java:27)
at org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:49)
... 6 more
TL;
localNameproperty of@JacksonXmlPropertyis not set, therequiredof@JsonPropertydoes not detect the property name of POJO.localNameattribute of@JacksonXmlPropertyhas the same function as https://github.com/FasterXML/jackson-annotations?tab=readme-ov-file#annotations-for-renaming-properties , but is used renaming of POJO properties. Not settinglocalNameshould not cause this error.