Ich versuche, einen Restdienstanruf in Java zu machen. Ich bin neu im Web und Rest Service. Ich habe Restdienst, der JSON als Antwort zurückgibt. Ich habe den folgenden Code, aber ich denke, dass es unvollständig ist, weil ich nicht weiß, wie man Ausgabe mit json verarbeitet.JSON-Antwort als Teil des Restaufrufs in Java abrufen
public static void main(String[] args) {
try {
URL url = new URL("http://xyz.com:7000/test/db-api/processor");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os = connection.getOutputStream();
//how do I get json object and print it as string
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
Bitte helfen Sie. Ich bin neu in Raststätten und JSON. Vielen Dank im Voraus.
Wenn Sie Frühling verwenden, es macht Ihnen das Leben leicht. Es führt eine HTTP-Anforderung aus, konvertiert die HTTP-Antwort in einen Objekttyp Ihrer Wahl und gibt dieses Objekt zurück. https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate –