title: Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object]
body: <p>I am trying to post an xml request to a third party server and expecting an xml response back.</p>
  
  <p>I am here by attaching the source code for configuring Cnfiguration,gateways &amp; channels, Customized Rest Template class and Test Client to execute. We have various message converters for XML Posting but despite that it is always saying no suitable HTTPMessage Converters</p>
  
  <pre><code>/**** Gateway &amp; Channel Configuration class****/
  
  @Configuration
  @MessagingGateway(name = "EntryGateway", defaultRequestChannel = "EntryChannel", defaultRequestTimeout = "2000", defaultReplyChannel = "ExitChannel", defaultReplyTimeout = "2000")
  public interface Gateway {
  
  
      @Gateway(requestChannel = "EntryChannel", requestTimeout = Constants.REQUEST_TIMEOUT_IN_MILLISECONDS, replyChannel = "ExitChannel", replyTimeout = Constants.RESPONSE_TIMEOUT_IN_MILLISECONDS)
      ReqResMessage sendRequest(ReqResMessage request);
  
  
  }
  
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.aopalliance.aop.Advice;
  import org.slf4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.http.HttpMethod;
  import org.springframework.http.MediaType;
  import org.springframework.http.converter.FormHttpMessageConverter;
  import org.springframework.http.converter.HttpMessageConverter;
  import org.springframework.http.converter.StringHttpMessageConverter;
  import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
  import org.springframework.integration.annotation.IntegrationComponentScan;
  import org.springframework.integration.annotation.ServiceActivator;
  import org.springframework.integration.channel.DirectChannel;
  import org.springframework.integration.config.EnableIntegration;
  import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
  import org.springframework.messaging.MessageChannel;
  import org.springframework.messaging.MessageHandler;
  import org.springframework.messaging.converter.MessageConverter;
  import org.springframework.oxm.Marshaller;
  import org.springframework.oxm.Unmarshaller;
  import org.springframework.oxm.jaxb.Jaxb2Marshaller;
  
  
  
  
  
  @Configuration
  @ComponentScan("package_name")
  @IntegrationComponentScan("package_name")
  @EnableIntegration
  @Import(value = ExternalSystemsConfiguration.class)
  @PropertySource(value = "classpath:properties", ignoreResourceNotFound = true)
  public class Configuration {
  
      private static final Logger logger = LogbackConfig.getApplicationLogger(POPConfiguration.class);
  
      @Autowired
      Environment env;
  
      @Autowired
      ExternalSystemsConfiguration externalSystemsConfiguration;
  
      /**
       * Entry channel to the messaging system through the gateway.
       * 
       * @return MessageChannel
       */
      @Bean
      public MessageChannel EntryChannel() {
          return new DirectChannel();
      }
  
      @Bean
      public MessageChannel RequestChannel() {
          return new DirectChannel();
      }
  
      @Bean
      public MessageChannel ResponseChannel() {
          return new DirectChannel();
      }
  
      @Bean
      public MessageChannel ExitChannel() {
          return new DirectChannel();
      }
  
      @Bean
      @ServiceActivator(inputChannel = "RequestChannel")
      public MessageHandler cmmHttpGateway() throws Exception {
  
  
  
          logger.debug("Entered Configuration httpGateway() ");
          List&lt;Advice&gt; retryAdvices = new ArrayList&lt;&gt;();
          retryAdvices.add(externalSystemsConfiguration.requestHandlerRetryAdvice());
          HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(env.getProperty("url")
                  );
  
  
           List&lt;HttpMessageConverter&lt;?&gt;&gt; converters = new ArrayList&lt;&gt;();
              converters.add(marshallingMessageConverter());
  
          handler.setMessageConverters(converters);
          handler.setOutputChannel(popResponseChannel());
          handler.setRequiresReply(true);
          //handler.setExtractPayload(true);
  
          // handler.
          handler.setHttpMethod(HttpMethod.POST);
  
          logger.debug("Exited Configuration httpGateway() ");
          return handler;
      }
      @Bean
      public MarshallingHttpMessageConverter marshallingMessageConverter() {
          return new MarshallingHttpMessageConverter(
              jaxb2Marshaller(),
              jaxb2Marshaller()
          );
      }
  
      @Bean
      public Jaxb2Marshaller jaxb2Marshaller() {
          System.out.println("jaxb2Marshaller");
          Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
          marshaller.setClassesToBeBound(new Class[]{
                  ReqResMessage.class,
                  ReqResBody.class,
                  ReqResHeader.class,
                  Request.class,
                  Response.class
  
  
          });
          return marshaller;
      }
  }
  
  
  
  
  
  /***** Rest Template Comfiguration ******/
  import java.io.IOException;
  import java.net.HttpURLConnection;
  import java.net.InetSocketAddress;
  import java.net.Proxy;
  import java.security.cert.X509Certificate;
  import java.util.ArrayList;
  import java.util.Base64;
  import java.util.List;
  import java.util.Map;
  import java.util.Queue;
  
  import javax.net.ssl.SSLException;
  import javax.net.ssl.SSLSession;
  import javax.net.ssl.SSLSocket;
  
  import org.apache.http.Header;
  import org.apache.http.HttpHeaders;
  import org.apache.http.HttpHost;
  import org.apache.http.HttpResponse;
  import org.apache.http.auth.AuthOption;
  import org.apache.http.auth.AuthScheme;
  import org.apache.http.auth.AuthScope;
  import org.apache.http.auth.MalformedChallengeException;
  import org.apache.http.auth.UsernamePasswordCredentials;
  import org.apache.http.client.AuthCache;
  import org.apache.http.client.AuthenticationStrategy;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.config.RequestConfig;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpHead;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.client.params.HttpClientParams;
  import org.apache.http.client.protocol.HttpClientContext;
  import org.apache.http.conn.params.ConnRoutePNames;
  import org.apache.http.conn.ssl.X509HostnameVerifier;
  import org.apache.http.impl.auth.BasicScheme;
  import org.apache.http.impl.client.BasicAuthCache;
  import org.apache.http.impl.client.BasicCredentialsProvider;
  import org.apache.http.impl.client.CloseableHttpClient;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.impl.client.DefaultRedirectStrategy;
  import org.apache.http.impl.client.HttpClientBuilder;
  import org.apache.http.impl.client.LaxRedirectStrategy;
  import org.apache.http.impl.client.ProxyAuthenticationStrategy;
  import org.apache.http.message.BasicHeader;
  import org.apache.http.protocol.HttpContext;
  import org.slf4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.http.MediaType;
  import org.springframework.http.client.ClientHttpRequestFactory;
  import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  import org.springframework.http.client.SimpleClientHttpRequestFactory;
  import org.springframework.http.converter.FormHttpMessageConverter;
  import org.springframework.http.converter.HttpMessageConverter;
  import org.springframework.http.converter.StringHttpMessageConverter;
  import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
  import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
  import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice;
  import org.springframework.oxm.Marshaller;
  import org.springframework.oxm.Unmarshaller;
  import org.springframework.oxm.jaxb.Jaxb2Marshaller;
  import org.springframework.retry.backoff.FixedBackOffPolicy;
  import org.springframework.retry.policy.SimpleRetryPolicy;
  import org.springframework.retry.support.RetryTemplate;
  import org.springframework.web.client.RestTemplate;
  
  
  @Configuration
  @PropertySource(value ="classpath:integration.properties", ignoreResourceNotFound =true)
  public class ExternalSystemsConfiguration {
  
      @Autowired
      Environment env;
  
  
      private static final Logger logger = LogbackConfig.getApplicationLogger(ExternalSystemsConfiguration.class);
  
  
  
  
  
      @Bean
      public RequestHandlerRetryAdvice requestHandlerRetryAdvice() {
          logger.debug("Entered RetryConfiguration requestHandlerRetryAdvice()");
          RequestHandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
          RetryTemplate retryTemplate = new RetryTemplate();
          FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
          SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
          simpleRetryPolicy.setMaxAttempts(Integer.parseInt(env.getProperty("retryAttempts")));
          fixedBackOffPolicy.setBackOffPeriod(Integer.parseInt(env.getProperty("backOffPolicy")));
          retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
          retryTemplate.setRetryPolicy(simpleRetryPolicy);
          retryAdvice.setRetryTemplate(retryTemplate);
          return retryAdvice;
      }
  
      /**
       * This method is used to create rest template with configurable read and connection timeouts.It is used by all http gateways.
       * @return RestTemplate
       */
      @SuppressWarnings("deprecation")
      @Bean
      public RestTemplate getRestTemplate(){
  
           RestTemplate restTemplate = new RestTemplate();
  
  
          BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
          credentialsProvider.setCredentials(new AuthScope("test.com", 443), new UsernamePasswordCredentials("testuser", "testpassword"));
           HttpHost target = new HttpHost("test", 443, "https");
  
           System.out.println("BASE64:"+credentialsProvider.toString());
          // Create AuthCache instance
          AuthCache authCache = new BasicAuthCache();
          // Generate BASIC scheme object and add it to the local auth cache
          BasicScheme basicAuth = new BasicScheme();
          authCache.put(target, basicAuth);
  
          // Add AuthCache to the execution context
          HttpClientContext context = HttpClientContext.create();
          context.setCredentialsProvider(credentialsProvider);
          context.setAuthCache(authCache);
  
          Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/xml");
          List&lt;Header&gt; headers = new ArrayList&lt;&gt;();
                  headers.add(header);
           HttpHost proxy = new HttpHost("", "", "http");
  
           RequestConfig config = RequestConfig.custom()
                   .setProxy(proxy)
                   .build();
           CloseableHttpClient  httpClient = HttpClientBuilder.create().setProxy(proxy).setDefaultHeaders(headers).setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
                                                  .setDefaultCredentialsProvider(credentialsProvider).setRedirectStrategy(new DefaultRedirectStrategy() {
  
                                                      private String[] REDIRECT_METHODS = new String[] { 
                                                          HttpGet.METHOD_NAME, HttpHead.METHOD_NAME 
                                                      };
  
                                                      @Override
                                                      protected boolean isRedirectable(String method) {
                                                          for (String m : REDIRECT_METHODS) {
                                                              if (m.equalsIgnoreCase(method)) {
                                                                  return true;
                                                              }
                                                          }
                                                          return false;
                                                      }
                                                  })
                                                  .build();
  
  
  
          restTemplate.setRequestFactory(new Conn());
      /*   List&lt;HttpMessageConverter&lt;?&gt;&gt; converters = new ArrayList&lt;&gt;();
              converters.add(marshallingMessageConverter());
              converters.add(new FormHttpMessageConverter());
              converters.add(new StringHttpMessageConverter());
              restTemplate.setMessageConverters(converters);*/
          return restTemplate;
      }
  
  
  
      public class Conn extends SimpleClientHttpRequestFactory {
  
          public Conn(){
              Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("", 8080));
              this.setProxy(proxy);
          }
          @Override
          protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
              // TODO Auto-generated method stub
              super.prepareConnection(connection, httpMethod);
  
              connection.setFollowRedirects(true);
  
              String userpassword = "testuser" + ":" + "testpassword";
              String encodedAuthorization = Base64.getEncoder().encodeToString(userpassword.getBytes());
              System.out.println("basic-----" + encodedAuthorization);
  
              connection.setDoOutput(true);
              connection.setRequestMethod("POST");
              connection.addRequestProperty("Authorization", "Basic " + encodedAuthorization);
              connection.addRequestProperty("content-type", "application/xml");
          }
  
      }
  }
  
  /***** Test Client to Post XML Request in the form of Java object as well as String *****/
  
  
   */
  
  
  
  
  @ContextConfiguration(classes = Configuration.class)
  @RunWith(SpringJUnit4ClassRunner.class)
  public class Test {
  
      private static Logger logger = LogbackConfig.getApplicationLogger(Test.class);
  
      @Autowired
      private Gateway cmmGateway;
  
      @Autowired
      RestTemplate template;
  
      @Test
      public void testJsonResponse() {
          String xml =     "&lt;?xml version=\"1.0\"?&gt;" 
                  +"&lt;Message&gt;"
                  + "&lt;Header&gt;&lt;NCPDPID&gt;3942100&lt;/NCPDPID&gt;&lt;SentTime&gt;2016-07-14 06:13:00&lt;/SentTime&gt;"
                  + "&lt;SenderID&gt;01hw320985&lt;/SenderID&gt;&lt;MaxRowCount&gt;500&lt;/MaxRowCount&gt;"
                  + "&lt;/Header&gt;&lt;Body&gt;&lt;Request&gt;&lt;Distance&gt;100.00&lt;/Distance&gt;&lt;LastName&gt;ALLEN&lt;/LastName&gt;"
                  + "&lt;FirstName&gt;&lt;/FirstName&gt;&lt;Gender&gt;&lt;/Gender&gt;&lt;Phone&gt;&lt;/Phone&gt;&lt;City&gt;&lt;/City&gt;"
                  + "&lt;State&gt;&lt;/State&gt;&lt;LicensedState&gt;&lt;/LicensedState&gt;" + "&lt;DEA&gt;&lt;/DEA&gt;"
                  + "" + "&lt;/Request&gt;"
                  + "&lt;/Body&gt;" + "&lt;/Message&gt;";/*
           ReqResMessage user=null;
          try{
          JAXBContext jaxbContext = JAXBContext.newInstance(ReqResMessage.class);
          Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
          InputStream is = new ByteArrayInputStream(xml.getBytes());
          user= (ReqResMessage) jaxbUnmarshaller.unmarshal(is);   
         // System.out.println("REquest****"+user.getBody().getRequest());
          }catch(Exception e){
              e.printStackTrace();
          }*/
          ReqResMessage re = new ReqResMessage();
          ReqResHeader header= new ReqResHeader();
          header.setPharmacyNCPDPID("3942100");
          header.setRowCount("500");
          header.setSenderID("01hw320985");
  
          Request request = new Request();
          request.setDistance("100.00");
          request.setLastName("ALLEN");
          ReqResBody body = new ReqResBody();
          body.setRequest(request);
          re.setBody(body);
          re.setHeader(header);
  
          //System.out.println("Before:"+System.currentTimeMillis());
          ReqResMessage response =    cmmGateway.sendRequest(re);
  
      }
  
  }
  </code></pre>