我想将一个不可变类型序列化为json和xml:
序列化的JSON如下:
{
"text" : "... the text..."
}
Run Code Online (Sandbox Code Playgroud)
序列化的xml如下:
<asText>_text_</asText>
Run Code Online (Sandbox Code Playgroud)
(注意文本是xml的元素文本)
java对象如下:
@JsonRootName("asText")
@Accessors(prefix="_")
public static class AsText {
@JsonProperty("text") @JacksonXmlText
@Getter private final String _text;
public AsText(@JsonProperty("text") final String text) {
_text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意_text属性是final(因此对象是不可变的)并且它被注释@JacksonXmlText以便被序列化为xml元素的文本
作为对象不可变,需要来自文本的构造函数,并且必须使用构造函数的参数进行注释 @JsonProperty
public AsText(@JsonProperty("text") final String text) {
_text = text;
}
Run Code Online (Sandbox Code Playgroud)
对JSON进行序列化/反序列化时,一切正常......在向/从XML序列化/反序列化时出现问题:
// create the object
AsText obj = new AsText("_text_");
// init the mapper
XmlMapper mapper = new XmlMapper();
// write as xml
String xml …Run Code Online (Sandbox Code Playgroud) 在Lucene的新4.4.0版本中,Near Real Time Manager(org.apache.lucene.search.NRTManage)已被ControlledRealTimeReopenThread取代
有没有人为新的ControlledRealTimeReopenThread用法提供一些示例代码?
编辑:我在下面回答我自己的问题