From 5089668633f96cf03aacb83475f106668ec05149 Mon Sep 17 00:00:00 2001 From: Beppe Vanrolleghem Date: Tue, 24 Mar 2020 10:53:21 +0100 Subject: [PATCH] backend get request --- .../stage/frontend/BasicRestController.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/com/frontend/stage/frontend/BasicRestController.java b/src/main/java/com/frontend/stage/frontend/BasicRestController.java index bc3662f..abd6ab7 100644 --- a/src/main/java/com/frontend/stage/frontend/BasicRestController.java +++ b/src/main/java/com/frontend/stage/frontend/BasicRestController.java @@ -4,6 +4,12 @@ package com.frontend.stage.frontend; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + @RestController public class BasicRestController { @@ -17,4 +23,32 @@ public class BasicRestController { public String index2() { return "it do work"; } + @RequestMapping("/stage-frontend/check") + public String getBackend() throws Exception { + return doRequest("http://stage-backend-service"); + } + + public String doRequest(String addr) throws Exception { + URL url = new URL(addr); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + int responseCode = con.getResponseCode(); + String responseVal; + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + responseVal = response.toString(); + } else { + throw new Exception("something went wrong"); + } + return responseVal; + } + + }