使用RestAPI在Java上通过Kaa平台发送通知

时间:2018-11-26 00:31:53

标签: java jersey 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>

谢谢您的阅读^^

1 个答案:

答案 0 :(得分:1)

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