<!CDATA[[]]>映射为""而不是null Jackson

huangapple go评论70阅读模式
英文:

<!CDATA[[]]> Mapping to "" instead of null Jackson

问题

我有一个从Kafka主题读取XML并转换为字符串格式的函数,然后我想将其转换为JSON对象。

示例XML:

&lt;Example&gt;
  &lt;Object&gt;
    &lt;Member1&gt;&lt;![CDATA[]]&lt;/Member1&gt;
    &lt;Member2&gt;&lt;![CDATA[someText]]&lt;/Member2&gt;
  &lt;/Object&gt;
&lt;/Example&gt;

然后我有一些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:

&lt;Example&gt;
  &lt;Object&gt;
    &lt;Member1&gt;&lt;![CDATA[]]&lt;/Member1&gt;
    &lt;Member2&gt;&lt;![CDATA[someText]]&lt;/Member2&gt;
  &lt;/Object&gt;
&lt;/Example&gt;

I then have POJO Classes such as: (with lombok getter/setter/toString and jackson annotation imports)

@Getter
@Setter
@ToString
@JacksonXMLRootElement(localName = &quot;Example&quot;)
public class TXML {

  @JacksonXmlProperty(localName = &quot;Object&quot;)
  private someObject object;
}
@Getter
@Setter
@ToString
public class someObject {

  @JacksonXmlProperty(localName = &quot;Member1&quot;)
  private String member1;

  @JacksomXmlProperty(localName = &quot;Member2&quot;)
  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
-&gt; object
    -&gt; member1 : &quot;&quot;
    -&gt; member2 : &quot;someText&quot;

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&lt;String, String&gt; {

	@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 = &quot;Member1&quot;)
	private String member1;

	@JsonDeserialize(converter = CDATAConverter.class)
	@JacksonXmlProperty(localName = &quot;Member2&quot;)
	private String member2;
}

huangapple
  • 本文由 发表于 2020年10月17日 02:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64394193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定