Отправить уведомление на платформе Kaa с помощью Java с помощью RestAPI - PullRequest
0 голосов
/ 26 ноября 2018

В документе 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 * 1007вернул код 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>

Спасибо за чтение ^^

1 Ответ

0 голосов
/ 01 декабря 2018

Я решил эту проблему !!!Попробуйте другой способ вызвать этот 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>
...