如何使用RestTemplate发布bean列表,但是bean必须具有字节数组?

时间:2019-05-22 07:47:46

标签: java spring rest spring-mvc

我有两个单独的项目(RestApi项目,客户端项目)。我有一个bean类(附件),它具有byte []。我想发布此bean添加列表集合(列表)。我发布了对象,但在发送的方法(processStart())中列表为空。注意:在客户端项目中,列表不为null。

**** RestApi项目****

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
   }
}

****客户项目****

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;
    }
 }

1 个答案:

答案 0 :(得分:0)

这类东西的约定是将二进制数据编码为Base64并将其作为字符串放置,我建议您这样做,超级容易:)

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