title: Send byte array and receive String through REST web service
body: <p>in my Spring Rest web service I send a file (even big size) as byte array but when I receive the information, the object is a String so when I make the cast from Object to byte[] I receive the following error:</p>
  
  <blockquote>
    <p>java.lang.ClassCastException: java.lang.String cannot be cast to [B</p>
  </blockquote>
  
  <p>The originl file is converted through</p>
  
  <p><code>Files.readAllBytes(Paths.get(path))</code> </p>
  
  <p>and this <code>byte[]</code> is filled in one object with a field <code>result</code> of Object type.
  When the Client retrieve this object and it gets <code>result</code> class with cast to <code>byte[]</code> it appears the above exception, this is the client code</p>
  
  <p><code>Files.write(Paths.get("test.txt"),((byte[])response.getResult()))</code>;</p>
  
  <p>If I use a cast to string and then to bytes the content of the file is different from original file. I don't care the file type, file content, I only have to copy from server to client directory
  How can I do?Thanks</p>
  
  <p>server class:</p>
  
  <pre><code>@Override
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public @ResponseBody Response getAcquisition(@RequestParam(value="path", defaultValue="/home") String path){
          try {
              byte[] file = matlabClientServices.getFile(path);
              if (file!=null){
                  FileTransfer fileTransfer= new FileTransfer(file, Paths.get(path).getFileName().toString());
                  return new Response(true, true, fileTransfer, null);
              }
              else 
                  return new Response(false, false, "File doesn't exist!", null);         
          } catch (Exception e) {
              ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
              LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
              return new Response(false, false, "Error during file retrieving!", errorResponse);
          }       
      }
  </code></pre>
  
  <p>and FileTransfer is:</p>
  
  <pre><code>    public class FileTransfer {
  
          private byte[] content;
          private String name;
  ..get and set
  </code></pre>
  
  <p>client class:</p>
  
  <pre><code>    @RequestMapping(value = "/", method = RequestMethod.GET)
  public @ResponseBody Response getFile(@RequestParam(value="path", defaultValue="/home") String path){
      RestTemplate restTemplate = new RestTemplate();
      Response response = restTemplate.getForObject("http://localhost:8086/ATS/client/file/?path={path}", Response.class, path);
      if (response.isStatus() &amp;&amp; response.isSuccess()){
          try {
              @SuppressWarnings("unchecked")
              LinkedHashMap&lt;String,String&gt; result= (LinkedHashMap&lt;String,String&gt;)response.getResult();
              //byte[] parseBase64Binary = DatatypeConverter.parseBase64Binary((String)fileTransfer.getContent());
              Files.write(Paths.get(result.get("name")), DatatypeConverter.parseBase64Binary(result.get("content"))); 
              return new Response(true, true, "Your file has been written!", null);
              } catch (IOException e) {
              return new Response(true, true, "Error writing your file!!", null);
          }
      }
      return response;
  }
  </code></pre>