bounty将在6天后过期。回答此问题可获得+50的声誉奖励。Codepressed正在寻找来自声誉良好来源的答案:解决这个问题。为什么这个调用在postman上工作,而不是在我的java应用程序上。我试过很多解决方案,比如使用messageConverter,@Data,修改ToString...
有一个我必须使用的Mail API,我不能通过我的Spring应用程序发送信息。当我在Postman上复制/粘贴我的HttpEntity的'body'时,它提供了完美的服务,但使用RestTemplate似乎无法工作。我甚至试图制作一个toString方法来匹配PostMan调用的格式,但即使这样也无法工作。这些是我的Java class和toString方法:
@Builder
public class CreateAndQueueMailRq {
private String application;
private String bcc;
private String body;
private String cc;
private String creationDate;
private String creationUser;
private String from;
private String fromName;
private String id;
private String market;
private String subject;
private String system;
private String to;
private String language;
private String type;
private String attachmentType;
private String attachmentFileName;
private byte[] content;
@Override
public String toString() {
return "{\"application\":\"" + application + "\","
+ "\"bcc\":\"" + bcc + "\","
+ "\"body\":\"" + body + "\","
+ "\"cc\":\"" + cc + "\","
+ "\"creationDate\":\"" + creationDate + "\","
+ "\"creationUser\":\"" + creationUser + "\","
+ "\"from\":\"" + from + "\","
+ "\"fromName\":\"" + fromName + "\","
+ "\"id\":\"" + id + "\","
+ "\"market\":\"" + market + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"system\":\"" + system + "\","
+ "\"to\":\"" + to + "\","
+ "\"language\":\"" + language + "\","
+ "\"type\":\"" + type + "\","
+ "\"attachmentType\":\"" + attachmentType + "\","
+ "\"attachmentFileName\":\"" + attachmentFileName + "\","
+ "\"content\":" + Arrays.toString(content) + "}";
}
这就是我如何执行我的呼叫:
public boolean sendMailWithAttachment(String body, String from, String fromName,
String id, String subject, String to, String language,
String filename, byte[] content, String cc, String bcc){
CreateAndQueueMailRq createAndQueueMailRq = CreateAndQueueMailRq.builder()
.application("appname")
.bcc("")
.body(body)
.cc("samplemail@gmail.com")
.creationDate(LocalDateTime.now().format(FORMATTER))
.creationUser(MailManagerConstants.COMING2)
.from(from)
.fromName(fromName)
.id(id)
.market("market_default")
.subject(subject)
.system("system_default")
.to("samplemail@gmail.com")
//.to("samplemail@gmail.com")
.language("ES")
.type(DEFAULT_TYPE)
.attachmentType(MailManagerConstants.TYPE_PDF)
.attachmentFileName(filename)
.content(content)
.build();
return generateRequest(createAndQueueMailRq, API_MAIL_MANAGER_WITH_ATTACHMENT);
}
private boolean generateRequest(CreateAndQueueMailRq createAndQueueMailRq, String api) {
//ObjectMapper objectMapper = new ObjectMapper();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
try {
//HttpEntity<String> request = new HttpEntity<>(objectMapper.writeValueAsString(createAndQueueMailRq), headers);
HttpEntity<CreateAndQueueMailRq> request = new HttpEntity<>(createAndQueueMailRq, headers);
System.out.println("Request: "+request.getBody());
ResponseEntity<?> response = restTemplate.exchange(api, HttpMethod.POST, request, String.class);
LOGGER.info(SUCCESS_MESSAGE);
return true;
}catch (HttpClientErrorException e){
LOGGER.info("STATUS CODE: "+e.getStatusCode());
LOGGER.info("RESPONSE BODY: "+e.getResponseBodyAsString());
LOGGER.info(ERROR_MESSAGE);
return false;
}
}
如果我复制粘贴e.getResponseBodyAsString(),它在Postman.
上工作
问题出在内容byte[]。
1条答案
按热度按时间vmdwslir1#
只需要在构造
HttpEntity
示例时显式提供字符串即可: