title: Spring restTemplate.exchange throwing XML parsererror at server side
body: <p>I am using restTemplate.exchange or restTemplate.postForObject method at client side where I am passing request with headers and authentication method. </p>
  
  <p>At server side with other system, it throws error as </p>
  
  <pre><code>  Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
    Message: Content is not allowed in prolog.
     at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604) ~[?:1.8.0_171]
     at org.glassfish.jersey.jaxb.internal.AbstractCollectionJaxbProvider.readFrom(AbstractCollectionJaxbProvider.java:344) ~[jersey-media-jaxb-2.24.1.jar:?]
  </code></pre>
  
  <p>Thus throwing error as </p>
  
  <pre><code>   ERROR com.....ExceptionHandler   - Exception occurred while processing the request! javax.ws.rs.BadRequestException: HTTP 400 Bad Request
  </code></pre>
  
  <p>What is going wrong here? 
  I have tried sending minimal params only mandatory ones, tried exchange() and postForObject() method. </p>
  
  <p>The request XML generated out of this httpEntity below works fine in postman</p>
  
  <p>here is my service class - </p>
  
  <pre><code>@Service("Service")
  public class Service {
  
  private static final Logger logger = LogManager.getLogger(Service.class);
  
  @Autowired
  private RestTemplate restTemplate;
  
  @SuppressWarnings("rawtypes")
  public String postRequest(String loanId, Request requestXml, Map&lt;String, String&gt; headersInfo) {
  
      HttpHeaders headers = new HttpHeaders();
  //  ResponseEntity&lt;GenericResponse&lt;Response&gt;&gt;    response = null;
      Response     response = null;
  //  String response = null;
      List&lt;String&gt; riskReductionIdList  = null;
      String riskReductionId  = null;
      String responseString = null;
  
      headers.setContentType(MediaType.APPLICATION_XML);
      headers.set("-External-System-ID", headersInfo.get("ExternalSystemId"));
      headers.set("-External-User-ID", headersInfo.get("ExternalUserId"));
      String plainCredentials=headersInfo.get("User") + ":" + headersInfo.get("Password");
  
      String base64Credentials = new String(Base64.encodeBase64(plainCredentials.getBytes()));
  
        byte[] encodedAuth = Base64.encodeBase64( 
                plainCredentials.getBytes(Charset.forName("US-ASCII")) );
  
      headers.add("Authorization", "Basic " + base64Credentials);
      headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
  
      StringWriter request = new StringWriter();
  
      try {
          JAXBContext jaxbContext = JAXBContext.newInstance(Request.class);
          Marshaller marshaller = jaxbContext.createMarshaller();
          marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
          marshaller.marshal(requestXml, request);
          logger.error(" request : " + request.toString());
  
      } catch (JAXBException e1) {
          logger.error("Exception happened in coverting the request for logging",e1);
      }
  
  
      HttpEntity&lt;?&gt; httpEntity = new HttpEntity&lt;Object&gt;(requestXml, headers);
  
      logger.error(" Request Body: " + httpEntity.getBody());
      logger.error(" Request headers: " + httpEntity.getHeaders());
  
      try{
      //  response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference&lt;GenericResponse&lt;Response&gt;&gt;() {} );
          response = restTemplate.postForObject(url, httpEntity, Response.class);
  
      }catch(HttpClientErrorException ex){
          logger.error(" Error while sending data to  for loan id : " + loanId + "\n Error : "+ ex.getMessage());
          logger.error(" responseBody : " + ex.getResponseBodyAsString() + "\n statusText : "+ ex.getStatusText() + "\n statusCode : "+ ex.getStatusCode());
  
      }catch(HttpServerErrorException ex){
          logger.error(" Error while sending data to  for loan id : " + loanId + "\n Error : "+ ex.getMessage());
          logger.error(" responseBody : " + ex.getResponseBodyAsString() + "\n statusText : "+ ex.getStatusText() + "\n statusCode : "+ ex.getStatusCode());
  
      }catch(Exception ex){
          logger.error(" Error while sending data to  for loan id : " + loanId + "\n Error : "+ ex.getMessage());
  
      }
  
      responseString = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?&gt; &lt;response&gt;     &lt;code&gt;0&lt;/code&gt;     &lt;status&gt;SUCCESS&lt;/status&gt;     &lt;messages/&gt;     &lt;payload&gt;        "
              + " &lt;riskReductionIds&gt;             &lt;riskReductionId&gt;10000&lt;/riskReductionId&gt;         &lt;/riskReductionIds&gt;     &lt;/payload&gt; &lt;/response&gt;";
  
      return riskReductionId;
  }
  
  }
  </code></pre>