强制JacksonXML将原始XML反序列化为String

时间:2018-08-28 14:05:00

标签: xml spring spring-boot json-deserialization jackson-dataformat-xml

我有XML请求字符串作为文本内容输入,但是Jackson将其标识为XML标签。我尝试使用JacksonXMLText,但这无济于事。

<Reference>
  <Request><?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34; ?><Quote 
  sess=&#34;JAKE_OSCAR_EU&#34; language=&#34;ENG&#34; Country=&#34;US&#34;> 
  </Quote> 
  </Request>
</Reference>

这让我感到异常

com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.

1 个答案:

答案 0 :(得分:2)

我认为您需要编写一个客户反序列化器,以读取要读取Raw的那部分树。这是一个示例实现:

@JsonSerialize(using = RawObjectSerializer.class)
@JsonDeserialize(using = RawObjectDeserializer.class)
public class RawObject {

    public final String value;

    public RawObject(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

公共类RawObjectDeserializer扩展了StdDeserializer {

public RawObjectDeserializer() {
    super(RawObject.class);
}

    @Override
    public RawObject deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        return new RawObject(parser.getCodec().readTree(parser).toString());
    }
}

https://dolzhenko.me/blog/2017-08-13-raw-jackson