杰克逊:Jackson-Dataformat-XML:当从XML到Java的必要性化时,JSONPROPERTY注释所需的属性被忽略
我使用更具体的Jackson-Dataformat-XML杰克逊有问题。
解析JSON时,会有一份检查属性的检查,这些属性用JSONPROPERTY注释,并具有true所需的Attibute。 如果缺少所需的属性,则会抛出带有消息“缺少必需的创建者属性”的不匹配的Inputexception。
解析XML时,没有检查所需的属性/元素。如果缺少所需的属性/元素,则没有例外。 添加DeserializationFeature.fail_on_missing_creator_properties没有任何区别。
有jsoncreator注释的构造函数,并带有JSONPROPERTY所需的属性。
当使用2.11.4版本2.11.4时,它是否可以使用2.12.0,
我需要对所需XML元素使用另一个注释?
示例类:
@JsonRootName(value = "bar")
public class Bar
{
@JsonProperty(value = "foo", required = true)
private int foo;
public Bar()
{}
@JsonCreator
public Bar(@JsonProperty(value = "foo", required = true) final int foo)
{
this.foo = foo;
}
public int getFoo()
{
return this.foo;
}
@Override
public int hashCode()
{
return Objects.hash(this.foo);
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof Bar))
{
return false;
}
final Bar other = (Bar) obj;
return this.foo == other.foo;
}
@Override
public String toString()
{
return "Bar: " + Objects.toString(this.foo);
}
}
JSON上的单元测试的示例,该单元测试呈现出预期的异常,因为属性foo缺少。
@Test(expected = JsonProcessingException.class)
public void deserializeJson() throws Exception
{
final ObjectMapper jsonMapper = new ObjectMapper();
final Bar expected = new Bar(123);
final Bar actual = jsonMapper.readValue("{}", Bar.class); // missing property foo in JSON string
}
XML上的单元测试示例,此单元测试不会引发异常
@Test(expected = JsonProcessingException.class)
public void deserializeXml() throws Exception
{
final XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);
final Bar expected = new Bar(123);
final Bar actual = xmlMapper.readValue("<bar></bar>", Bar.class); // missing element foo in XML string
Assert.fail("Expected com.fasterxml.jackson.databind.exc.MismatchedInputException: Missing required creator property 'foo' (index 0)");
}
I have a problem using Jackson, more specific jackson-dataformat-xml.
When JSON is parsed, there is a check on properties that are annotated with JsonProperty with attibute required to true.
If the required property is missing, a MismatchedInputException with message "Missing required creator property" is thrown.
When XML is parsed, there is no check on required properties/element. If the required property/element is missing, there is no exception.
Adding DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES doesn't make any difference.
There is constructor is annotated with JsonCreator and with the required property annotated with JsonProperty.
It worked when jackson-dataformat-xml version 2.11.4 was used, it doesn't work since version 2.12.0
Do I need to use another annotation for a required XML element?
Example class:
@JsonRootName(value = "bar")
public class Bar
{
@JsonProperty(value = "foo", required = true)
private int foo;
public Bar()
{}
@JsonCreator
public Bar(@JsonProperty(value = "foo", required = true) final int foo)
{
this.foo = foo;
}
public int getFoo()
{
return this.foo;
}
@Override
public int hashCode()
{
return Objects.hash(this.foo);
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof Bar))
{
return false;
}
final Bar other = (Bar) obj;
return this.foo == other.foo;
}
@Override
public String toString()
{
return "Bar: " + Objects.toString(this.foo);
}
}
Example of unit test on JSON, this unit test throws an exception as expected, because property foo is missing.
@Test(expected = JsonProcessingException.class)
public void deserializeJson() throws Exception
{
final ObjectMapper jsonMapper = new ObjectMapper();
final Bar expected = new Bar(123);
final Bar actual = jsonMapper.readValue("{}", Bar.class); // missing property foo in JSON string
}
Example of unit test on XML, this unit test does not throw an exception
@Test(expected = JsonProcessingException.class)
public void deserializeXml() throws Exception
{
final XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);
final Bar expected = new Bar(123);
final Bar actual = xmlMapper.readValue("<bar></bar>", Bar.class); // missing element foo in XML string
Assert.fail("Expected com.fasterxml.jackson.databind.exc.MismatchedInputException: Missing required creator property 'foo' (index 0)");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论