title: Resttemplate : HttpMessageNotWritableException: No serializer found for class java.io.ByteArrayInputStream
body: <p>Using Spring Integration I upload a file via HTTP POST and route it to a service activator.</p>
  
  <p>In the service activator, I make a call using RestTemplate to another server where to dump the file but I can't figure out why I get the following error for the following code:</p>
  
  <p>What I don't understand is why I get the exception below when I call RestTemplate.exchange()</p>
  
  <pre><code>at pdi.integration.MultipartReceiver.printMultiPartContent(MultipartReceiver.java:41)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
  </code></pre>
  
  <p>xml-config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:int="http://www.springframework.org/schema/integration"
         xmlns:int-http="http://www.springframework.org/schema/integration/http"
         xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd"&gt;
  
      &lt;bean id="byteArrayHttpMessageConverter"
            class="org.springframework.http.converter.ByteArrayHttpMessageConverter"&gt;
      &lt;/bean&gt;
  
      &lt;bean id="formHttpMessageConverter"
            class="org.springframework.http.converter.FormHttpMessageConverter"&gt;
      &lt;/bean&gt;
  
      &lt;bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/&gt;
  
      &lt;bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper"&gt;
          &lt;property name="inboundHeaderNames" value="*"/&gt;
          &lt;property name="outboundHeaderNames" value="*"/&gt;
          &lt;property name="userDefinedHeaderPrefix" value=""/&gt;
      &lt;/bean&gt;
  
      &lt;int:channel id="http.frontend.rx"/&gt;
      &lt;int:channel id="http.frontend.tx"/&gt;
      &lt;int:channel id="http.backend.mysql.rx"/&gt;
      &lt;int:channel id="http.backend.mysql.tx"/&gt;
      &lt;int:channel id="http.backend.mongo.rx"/&gt;
      &lt;int:channel id="http.backend.mongo.tx"/&gt;
      &lt;int:channel id="http.backend.file.tx"/&gt;
  
      &lt;int-http:inbound-gateway
              id="frontEndToMySQLXX"
              request-channel="http.frontend.rx"
              reply-channel="http.frontend.tx"
              header-mapper="headerMapper"
              path="/gateway"
              supported-methods="GET,POST,PUT,DELETE"/&gt;
  
      &lt;int:router id="frontEndRouter" input-channel="http.frontend.rx" expression="headers.service"&gt;
          &lt;int:mapping value="json" channel="http.backend.mysql.tx" /&gt;
          &lt;int:mapping value="file" channel="http.backend.mongo.tx" /&gt;
          &lt;int:mapping value="upload" channel="http.backend.file.tx" /&gt;
      &lt;/int:router&gt;
  
      &lt;!-- removed : message-converters="formHttpMessageConverter,byteArrayHttpMessageConverter" --&gt;
      &lt;int-http:outbound-gateway
              id="toMongoDB"
              request-channel="http.backend.mongo.tx"
              reply-channel="http.backend.mongo.rx"
              url="http://localhost:5050/api/{path}"
              http-method-expression="headers.http_requestMethod"
              header-mapper="headerMapper"
              expected-response-type="byte[]"&gt;
          &lt;int-http:uri-variable name="path" expression="headers['urlpath']"/&gt;
      &lt;/int-http:outbound-gateway&gt;
  
      &lt;int-http:outbound-gateway
              id="toMySQLDB"
              request-channel="http.backend.mysql.tx"
              reply-channel="http.backend.mysql.rx"
              url="http://localhost:7070/api/{path}"
              http-method-expression="headers.http_requestMethod"
              expected-response-type="java.lang.String"
              charset="UTF-8"&gt;
          &lt;int-http:uri-variable name="path" expression="headers['urlpath']"/&gt;
      &lt;/int-http:outbound-gateway&gt;
  
      &lt;int:service-activator
          id="MySQLToFrontEnd"
          input-channel="http.backend.mysql.rx"
          output-channel="http.frontend.tx"
          ref="messageService"
          method="printContent"&gt;
      &lt;/int:service-activator&gt;
  
      &lt;int:service-activator
          id="MongoToFrontEnd"
          input-channel="http.backend.file.tx"
          output-channel="http.frontend.tx"
          ref="multipartReceiver"
          method="printMultiPartContent"&gt;
      &lt;/int:service-activator&gt;
  
   &lt;/beans&gt;
  </code></pre>
  
  <p>bean used by service activator</p>
  
  <pre><code>@Component
  public class MultipartReceiver {
  
      public void printMultiPartContent(LinkedMultiValueMap&lt;String, Object&gt; multipartRequest){
  
          System.out.println("### Successfully received multipart request ###");
          for (String elementName : multipartRequest.keySet()) {
              if (elementName.equals("file")){
                  System.out.println("\t" + elementName + " - as UploadedMultipartFile: " +
                          ((UploadedMultipartFile) multipartRequest
                                  .getFirst("file")).getOriginalFilename());
              }
          }
  
          RestTemplate template = new RestTemplate();
          String uri = "http://localhost:5050/api/upload";
          MultiValueMap map = new LinkedMultiValueMap&lt;String,Object&gt;();
          map.add("file", multipartRequest.getFirst("file"));
          HttpHeaders headers = new HttpHeaders();
          headers.set("Content-Type", "multipart/form-data");
          HttpEntity request = new HttpEntity(map, headers);
          ResponseEntity&lt;byte[]&gt; response = template.exchange(uri, HttpMethod.POST, request, byte[].class);
  
      }
  
  }
  </code></pre>
  
  <p>stacktrace :: <a href="http://pastebin.com/5Wa9VaRb" rel="nofollow">http://pastebin.com/5Wa9VaRb</a></p>
  
  <p>working code ::</p>
  
  <pre><code>public void printMultiPartContent(LinkedMultiValueMap&lt;String, Object&gt; multipartRequest) throws IOException {
  
          final String filename = ((UploadedMultipartFile) multipartRequest.getFirst("file")).getOriginalFilename();
  
          RestTemplate template = new RestTemplate();
          MultiValueMap&lt;String, Object&gt; multipartMap = new LinkedMultiValueMap&lt;String, Object&gt;();
          multipartMap.add("name", filename);
          multipartMap.add("filename", filename);
  
          byte[] bytes = ((UploadedMultipartFile) multipartRequest.getFirst("file")).getBytes();
          ByteArrayResource contentsAsResource = new ByteArrayResource(bytes){
              public String getFilename(){
                  return filename;
              }
          };
  
          multipartMap.add("file", contentsAsResource);
          String result = template.postForObject("http://localhost:5050/api/upload", multipartMap, String.class);
          System.out.println(result);
  
      }
  </code></pre>