title: Spring RestTemplate cannot unmarshal XML containing "<!DOCTYPE ... dtd>"
body: <p>I call an old web service provided by a third party. I am using Spring <code>RestTemplate</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>HttpEntity&lt;MyRequest&gt; requestHttpEntity = new HttpEntity&lt;&gt;(requestBody, headers);
  MyResponse response = restTemplate.postForEntity(url, requestHttpEntity, MyResponse.class);
  </code></pre>
  
  <p>I receive an XML (which format I cannot influence, it's a third party service) as a response:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;!DOCTYPE MyResponse SYSTEM "http://example.com:8080/some/path/MyResponse.dtd"&gt;
  
  &lt;MyResponse&gt;
      ...
  &lt;/MyResponse&gt;
  </code></pre>
  
  <p>The <code>postForEntity()</code> method throws the exception</p>
  
  <pre class="lang-none prettyprint-override"><code>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.]
  </code></pre>
  
  <p>I found the only sensible reference to the <code>http://apache.org/xml/features/disallow-doctype-decl</code> feature here: <a href="https://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl" rel="nofollow noreferrer">https://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl</a></p>
  
  <p><strong>Question: How can I customize the unmarshaling without completely avoiding the automagic behavior of Spring RestTemplate?</strong> I want to force the unmarshaler to accept XML containing  element with DTD reference.</p>
  
  <p>This question is strongly related to my other question <a href="https://stackoverflow.com/q/47836310/2886891">How to customize automatic marshaling in Spring RestTemplate to produce/modify XML headers (encoding, DOCTYPE)</a>, but the solution proposed there is not easily applicable here.</p>