试图 call/post 第三方 api 在 java spring
Trying to call/post a third party api in java spring
我的问题是当我尝试此操作时出现媒体类型错误,然后我更改了 header。现在我收到 500 错误。问题不在于 api,在 postman 上它完美地工作,请求 post 时我的代码是否做错了什么?
我的object模特
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 = "[chrispotjnr@gmail.com,chris@mqattach.com]";
this.eventCode = "DEFAULT";
this.metaData = metaData;
}
}
我的控制器
它应该发送带有 object body 的 post 请求,电子邮件已发送
@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());
}
postmanImage
您必须通过 ObjectMapper
将 EmailModel
转换为 json
格式
ObjectMapper objectMapper = new ObjectMapper();
String data = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(em);
并将 POST
更改为:
.POST(HttpRequest.BodyPublishers.ofString(data))
详细了解 ObjectMapper
我的问题是当我尝试此操作时出现媒体类型错误,然后我更改了 header。现在我收到 500 错误。问题不在于 api,在 postman 上它完美地工作,请求 post 时我的代码是否做错了什么?
我的object模特
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 = "[chrispotjnr@gmail.com,chris@mqattach.com]";
this.eventCode = "DEFAULT";
this.metaData = metaData;
}
}
我的控制器 它应该发送带有 object body 的 post 请求,电子邮件已发送
@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());
}
postmanImage
您必须通过 ObjectMapper
EmailModel
转换为 json
格式
ObjectMapper objectMapper = new ObjectMapper();
String data = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(em);
并将 POST
更改为:
.POST(HttpRequest.BodyPublishers.ofString(data))
详细了解 ObjectMapper