Getting a JSON response as part of a Rest call in Java

I am trying to make a rest service call in Java. I am new to networking and leisure. I have a holiday service that returns json as a response. I have the following code, but I think it is incomplete, because I do not know how to handle the output using json.

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); } } 

Please, help. I am new to leisure services and json. Thank you very much in advance.

+6
source share
4 answers

Since this is a PUT request, you are missing a few words here:

 OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); // The input you need to pass to the webservice os.flush(); ... BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); // Getting the response from the webservice String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); // Instead of this, you could append all your response to a StringBuffer and use `toString()` to get the entire JSON response as a String. // This string json response can be parsed using any json library. Eg. GSON from Google. } 

Check out this one for a clearer picture of getting web services.

+2
source

Your code is mostly correct, but there is an error in OutputStream . As P .J said, an OutputStream is required to pass a request to the server to the body. If your rest service does not require any organ, you do not need to use this one.

To read the server, you need to use an InputStream (RJ will also show you an example):

 try (InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();) { byte[] buf = new byte[512]; int read = -1; while ((read = inputStream.read(buf)) > 0) { byteArrayOutputStream.write(buf, 0, read); } System.out.println(new String(byteArrayOutputStream.toByteArray())); } 

This method is good if you do not want it to depend on the libraries of the third part. Therefore, I recommend you take a look at Jersey - a very nice library with lots of very useful features.

  Client client = JerseyClientBuilder.newBuilder().build(); Response response = client.target("http://host:port"). path("test").path("db-api").path("processor").path("packages"). request().accept(MediaType.APPLICATION_JSON_TYPE).buildGet().invoke(); System.out.println(response.readEntity(String.class)); 
+2
source

Since your Content-Type is a / json application, you can directly send a response to a JSON object, e.g.

 JSONObject recvObj = new JSONObject(response); 
0
source
 JsonKey jsonkey = objectMapper.readValue(new URL("http://echo.jsontest.com/key/value/one/two"), JsonKey.class); System.out.println("jsonkey.getOne() : "+jsonkey.getOne()) 
-1
source

Source: https://habr.com/ru/post/954757/


All Articles