我称之为第三方提供的旧网络服务。我正在使用Spring RestTemplate
:
HttpEntity<MyRequest> requestHttpEntity = new HttpEntity<>(requestBody, headers);
MyResponse response = restTemplate.postForEntity(url, requestHttpEntity, MyResponse.class);
我收到一个XML(我不能影响的格式,它是第三方服务)作为回复:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE MyResponse SYSTEM "http://example.com:8080/some/path/MyResponse.dtd">
<MyResponse>
...
</MyResponse>
postForEntity()
方法抛出异常
org.springframework.web.client.RestClientException:
Error while extracting response for type [class com.example.MyResponse] and content type [text/xml;charset=ISO-8859-1];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException:
Could not unmarshal to [class com.example.MyResponse]: null;
nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 10;
DOCTYPE is disallowed when the feature
"http://apache.org/xml/features/disallow-doctype-decl" set to true.]
我在这里找到了http://apache.org/xml/features/disallow-doctype-decl
特征的唯一明智的参考:https://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
问题:如何在不完全避免Spring RestTemplate的自动行为的情况下自定义解组?我想强制unmarshaler接受包含DTD引用的XML元素。
这个问题与我的其他问题How to customize automatic marshaling in Spring RestTemplate to produce/modify XML headers (encoding, DOCTYPE)密切相关,但是提出的解决方案在这里并不容易适用。