gpt4 book ai didi

java - 如何使用 RestTemplate 发布 bean 列表,但 bean 必须为字节数组?

转载 作者:行者123 更新时间:2023-12-02 05:22:19 33 4
gpt4 key购买 nike

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

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

最佳答案

此类内容的惯例是将二进制数据编码为 Base64 并将其作为字符串,我建议您这样做, super 简单:)

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

关于java - 如何使用 RestTemplate 发布 bean 列表,但 bean 必须为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56251780/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com