我的問題是當我嘗試這個時,我得到一個媒體型別錯誤,然后我更改了標題。現在我收到 500 錯誤。問題不在于 api,在郵遞員上它作業得很好,我在請求帖子時是否在我的代碼中做錯了什么?
我的物件模型
public class EmailModel {
private String module;
private String notificationGroupType;
private String notificationGroupCode;
private String notificationType;
private String inLineRecipients;
private String eventCode;
private HashMap<String, Object> metaData;
public EmailModel() {
this.module = "CORE";
this.notificationGroupType = "PORTAL";
this.notificationGroupCode = "DEFAULT";
this.notificationType = "EMAIL";
this.inLineRecipients = "[[email protected],[email protected]]";
this.eventCode = "DEFAULT";
this.metaData = metaData;
}
}
我的控制器它應該發送一個帶有物件正文的發布請求,電子郵件被發送
@RequestMapping(value = "test", method = RequestMethod.Post)
public void post() throws Exception {
String uri = "TestUrl";
EmailModel em = new EmailModel();
EmailModel data = em;
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.headers("Content-Type", "application/json")
.uri(URI.create(uri))
.POST(HttpRequest.BodyPublishers.ofString(String.valueOf(data)))
.build();
HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.discarding());
System.out.println(em);
System.out.println(response.statusCode());
}
郵遞員圖片
uj5u.com熱心網友回復:
您必須通過以下方式轉換EmailModel為json格式ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
String data = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(em);
并更改POST為:
.POST(HttpRequest.BodyPublishers.ofString(data))
查看更多關于ObjectMapper
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464398.html
