英文:
<!CDATA[[]]> Mapping to "" instead of null Jackson
问题
我有一个从Kafka主题读取XML并转换为字符串格式的函数,然后我想将其转换为JSON对象。
示例XML:
<Example>
<Object>
<Member1><![CDATA[]]</Member1>
<Member2><![CDATA[someText]]</Member2>
</Object>
</Example>
然后我有一些POJO类,如下所示:(具有lombok的getter/setter/toString和jackson注解导入)
@Getter
@Setter
@ToString
@JacksonXMLRootElement(localName = "Example")
public class TXML {
@JacksonXmlProperty(localName = "Object")
private someObject object;
}
@Getter
@Setter
@ToString
public class someObject {
@JacksonXmlProperty(localName = "Member1")
private String member1;
@JacksomXmlProperty(localName = "Member2")
private String member2;
}
然后,我使用MappingJackson2XmlHttpMessageConverter来获取对象映射器,然后使用它将XML字符串映射到Example类。
@Autowired ObjectMapper xmlMapper;
@Autowired
private MappingJackson2XmlHttpMessageConverter xmlConverter;
...
xmlMapper = xmlConverter.getObjectMapper();
Example example = xmlMapper.readValue(xmlString, Example.class);
这将生成一个带有以下内容的Example类:
Example
-> object
-> member1 : ""
-> member2 : "someText"
我希望member1为null,而不是空字符串。如何实现这一点?
英文:
I have a function that reads XML from a Kafka topic into String format, which I then want to convert to a JSON Object.
Example XML:
<Example>
<Object>
<Member1><![CDATA[]]</Member1>
<Member2><![CDATA[someText]]</Member2>
</Object>
</Example>
I then have POJO Classes such as: (with lombok getter/setter/toString and jackson annotation imports)
@Getter
@Setter
@ToString
@JacksonXMLRootElement(localName = "Example")
public class TXML {
@JacksonXmlProperty(localName = "Object")
private someObject object;
}
@Getter
@Setter
@ToString
public class someObject {
@JacksonXmlProperty(localName = "Member1")
private String member1;
@JacksomXmlProperty(localName = "Member2")
private String member2;
}
I am then using a MappingJackson2XmlHttpMessageConverter to get an Object Mapper, and using that to map the XML string to an Example class.
@Autowired ObjectMapper xmlMapper;
@Autowired
private MappingJackson2XmlHttpMessageConverter xmlConverter;
...
xmlMapper = xmlConverter.getObjectMapper();
Example example = xmlMapper.readValue(xmlString, Example.class);
This results in an Example Class with the following:
Example
-> object
-> member1 : ""
-> member2 : "someText"
I want member1 to be null, instead of an empty string. How can I accomplish this?
答案1
得分: 0
您需要实现自己的 `com.fasterxml.jackson.databind.util.Converter`,它将在反序列化后但在设置为所需形式之前将您的值进行转换。
示例实现:
class CDATAConverter implements Converter<String, String> {
@Override
public String convert(String value) {
return (value == null || value.length() == 0) ? null : value;
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructType(String.class);
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructType(String.class);
}
}
并且您需要指示 `Jackson` 使用它:
@Getter
@Setter
@ToString
class SomeObject {
@JsonDeserialize(converter = CDATAConverter.class)
@JacksonXmlProperty(localName = "Member1")
private String member1;
@JsonDeserialize(converter = CDATAConverter.class)
@JacksonXmlProperty(localName = "Member2")
private String member2;
}
英文:
You need to implement your own com.fasterxml.jackson.databind.util.Converter
which will convert your value after deserialisation but before setting to required form.
Example implementation:
class CDATAConverter implements Converter<String, String> {
@Override
public String convert(String value) {
return (value == null || value.length() == 0) ? null : value;
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructType(String.class);
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructType(String.class);
}
}
And you need to instruct Jackson
to use it:
@Getter
@Setter
@ToString
class SomeObject {
@JsonDeserialize(converter = CDATAConverter.class)
@JacksonXmlProperty(localName = "Member1")
private String member1;
@JsonDeserialize(converter = CDATAConverter.class)
@JacksonXmlProperty(localName = "Member2")
private String member2;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论