简体   繁体   中英

How to post bean List using RestTemplate, but bean have to a byte array?

I have two separate projects(RestApi Project,Client Project). I have a bean class(Attachment) and it have byte[ ]. I want to post this bean add List collection(List). I posted object but list is null in sended method(processStart()). Note:List is not null in client project.

****RestApi Project****

public class Attachment {
    private String fileName;
    private String extension;
    private byte[] file;

    //getter and setter
}

public class OnlineRuhsatBean {
    private List<Attachment> attachmentList = new ArrayList<>(); 

    //getter and setter
}


@RestController
@RequestMapping("/api/online")
public class OnlineRestService {

   @PostMapping
   public ResponseEntity<String> processStart(@RequestBody OnlineRuhsatBean onlineRuhsatBean){
      //some codes
   }
}

****Client Project ****

public class Attachment {
    private String fileName;
    private String extension;
    private byte[] file;

    //getter and setter
} 



public class Ruhsat {
    private List<Attachment> attacment;

    //getter and setter
}


public class EbysRestClient {
 private HttpHeaders postHeader (){
    HttpHeaders headers = new HttpHeaders();
    headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 
    Safari/537.36");
    headers.add("X-CSRF-TOKEN",  csrfToken); 
    headers.add("JSESSIONID",  jSessionId);
    headers.add("Cookie",  cookie);
    headers.add("Content-Type",  "application/json");

    return headers;
 }


 public String postRuhsat (Ruhsat ruhsat){
    RestTemplate restTemplate = new RestTemplate();

    String applyNum = "";  
    HttpEntity<Ruhsat> request = new HttpEntity<>(ruhsat,postHeader());
    ResponseEntity response = restTemplate.exchange("localhost:8080/ebys/api/online/", HttpMethod.POST, request, String.class);
    //ResponseEntity response = restTemplate.postForObject(appProperties.getRuhsatRestUrl(), request, ResponseEntity.class);
    //I try above code even so null in restapi project
    if (response.getStatusCode() == HttpStatus.OK){
        applyNum = response.getBody().toString();
        postFile(ruhsat, applyNum);
    }

    return applyNum;
    }
 }

The convention for that kind of stuff is to encode binary data to Base64 and put it as a string, I suggest you do that, super easy :)

https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM