http Post auf einen Grafana Server

Fohnbit

Top Contributor
Hallo!

Ich möchte meinen Grafana Server gerne über die API aus meinem Javacode zugreifen.
Leider erhalte ich bei "http.getInputStream()" keinerlei Ausgabe, da wohl der Aufbau falsch ist.

Laut Doku von Grafana:
1701948982722.png

habe ich diesen Code:
Java:
@Override
    public void run() {
        try {
            URL url = new URL("https://data.copmany.com/api/dashboards/db");
            URLConnection con = url.openConnection();
            HttpURLConnection http = (HttpURLConnection) con;
            http.setRequestMethod("POST");
            http.setDoOutput(true);

            String out = "{\"dashboard\": {\"id\": null,\"uid\": null,"
                    + "\"title\": \"Production Overview\",\"tags\": [ \"templated\" ],"
                    + "\"timezone\": \"browser\",\"schemaVersion\": 16,\"refresh\": \"25s\""
                    + "},\"message\": \"Made changes to xyz\",\"overwrite\": false"
                    + "}".getBytes(StandardCharsets.UTF_8);
            int length = out.length();

            http.setFixedLengthStreamingMode(length);
            http.setRequestProperty("Content-Type", "application/json");
            http.setRequestProperty("Accept", "application/json");
            http.setRequestProperty("Authorization", "Bearer glsa_bHDI6caI309c9bO6w5R1_fd693d02");
            http.connect();
            try (OutputStream os = http.getOutputStream()) {
                os.write(out.getBytes(StandardCharsets.UTF_8));
            }
            // Do something with http.getInputStream()
            BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
            String decodedString;
            while ((decodedString = in.readLine()) != null) {
                System.out.println(decodedString);
            }
            in.close();

        } catch (Exception e) {

        }

    }

Sieht jemand einen Fehler?
 
Nimm doch gleich einen REST-Client, nicht so ein gemurkse. Statt eines endlos verketteten Strings kann man auch eine Java-Klasse verwenden. Jackson erzeugt das JSON automatisch.
XML:
<!-- maven -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>3.1.4</version>
</dependency>
Java:
public class RestClient {
 
    private static final String REST_URI
      = "http://localhost:8082/spring-jersey/resources/employees";
 
    private Client client = ClientBuilder.newClient();

    public Employee getJsonEmployee(int id) {
        return client
          .target(REST_URI)
          .path(String.valueOf(id))
          .request(MediaType.APPLICATION_JSON)
          .get(Employee.class);
    }

    public Response createJsonEmployee(Employee emp) {
      return client
        .target(REST_URI)
        .request(MediaType.APPLICATION_JSON)
        .post(Entity.entity(emp, MediaType.APPLICATION_JSON));
    }
 
}
 
Hallo und vielen Dank! ich erstelle eine kleine OSGi Application.
Da scheiterte ich beim hinzufügen von Maven Repositories.

Wenn ich die .jar`s im Netz downloade, fehlen mir oft die Abhängigkeiten.
Hättest du eventuell einen Tipp wie ich einem OSGi Repository in Eclipse das Maven hinzufügen kann?
Danke!
 

Zurück
Oben