java - 通过 Java 使用 RestAPI 在 Kaa 平台中发送通知
问题描述
在 Kaa 文档中,如果我想发送通知,这里是提供调用的链接 rest api:https ://kaaproject.github.io/kaa/docs/v0.10.0/Programming-guide/Server-REST-APIs /#!/Notifications/sendNotification
当我使用邮递员调用该 api 时 - 一切都像这样 https://ctninhkieu-my.sharepoint.com/:i:/g/personal/ltthanh_ctninhkieu_onmicrosoft_com/ERK5TH8cEE5Hn4hgZ0IHsqgBSzePovlDqD4eUD9q68MUrQ?e=fVlFec
但是当我编写 java 代码用 glassfish jersey 调用它时,它返回 415 代码:
InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/kaaAdmin/rest/api/sendNotification, status=415, reason=Unsupported Media Type}}
这是我的代码:
String API_URI = "http://localhost:8080/kaaAdmin/rest/api/sendNotification";
Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
MultiPart multiPart = new FormDataMultiPart()
.bodyPart(new FileDataBodyPart("notification", new File("files/notification.json")))
.bodyPart(new FileDataBodyPart("file", new File("files/file.json")));
Response response = client.target(API_URI)
.request()
.header("Authorization", "Basic AAAAAAAAAAAAAA")
.post(Entity.entity(multiPart, multiPart.getMediaType()));
System.out.println(response.toString());
和 Maven 存储库
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
谢谢你的阅读^^
解决方案
我解决了这个问题!!!尝试另一种调用此 API 的方法:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/kaaAdmin/rest/api/sendNotification");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("notification", new File("files/notification.json"),
ContentType.APPLICATION_JSON, "notification.json");
builder.addBinaryBody("file", new File("files/file.json"),
ContentType.APPLICATION_OCTET_STREAM, "file.json");
HttpEntity multipart = builder.build();
httpPost.addHeader("Authorization", "Basic AAAAAAAAAAA");
httpPost.setEntity(multipart);
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
client.close();
有两个依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.6</version>
</dependency>
推荐阅读
- amazon-web-services - AWS VPC 已删除/待处理警报
- c++ - 默认重新散列后的 std::unordered_map<> bucket_count()
- r - 在 RMarkdown 中显示可变长度的输出
- php - WORDPRESS:将自定义帖子类型删除同步到 wordpress 帐户
- php - 在 PHP 中显示最近从远程 FTP 服务器上传的图像
- laravel - Laravel Vue.Js中外键为空时的关系错误
- python - 程序在给定的数字中找到一个不同的均匀度,并返回这个数字的位置
- php - 有条件地加载公共和管理代码,AJAX 在 WordPress 的两边都工作
- ruby-on-rails - 我应该使用 Rails 来保持一致性吗?(对于 ETL 项目)
- php - Vue + Laravel 不发送电子邮件 - 没有收到任何错误