首页 > 解决方案 > 通过 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>

谢谢你的阅读^^

标签: javajerseykaa

解决方案


我解决了这个问题!!!尝试另一种调用此 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>

推荐阅读