Godogo
Godogo

Reputation: 27

Printing ArrayList values in SpringBoot

I created an ArrayList with the json values from an Rest API.

This is the code to read the Rest API:

@RestController
public class exemploclass {
    
    @RequestMapping(value="/vectors")
    //@Scheduled(fixedRate = 5000)
    public ArrayList<StateVector> getStateVectors() throws Exception {
        
        ArrayList<StateVector> vectors = new ArrayList<>();
        
        String url = "https://opensky-network.org/api/states/all?lamin=41.1&lomin=6.1&lamax=43.1&lomax=8.1";
        //String url = "https://opensky-network.org/api/states/all?lamin=45.8389&lomin=5.9962&lamax=47.8229&lomax=10.5226";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        //add request header
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
        }

        in.close();
        
        
        JSONObject myResponse = new JSONObject(response.toString());
        JSONArray states = myResponse.getJSONArray("states");
        System.out.println("result after Reading JSON Response");
            
        for (int i = 0; i < states.length(); i++) {
            
            JSONArray jsonVector = states.getJSONArray(i);
            String icao24 = jsonVector.optString(0);
            String callsign = jsonVector.optString(1);
            String origin_country = jsonVector.optString(2);
            Boolean on_ground = jsonVector.optBoolean(8);
            
            //System.out.println("icao24: " + icao24 + "| callsign: " + callsign + "| origin_country: " + origin_country + "| on_ground: " + on_ground);
            //System.out.println("\n");
            
            StateVector sv = new StateVector(icao24, callsign, origin_country, on_ground);
            vectors.add(sv);
  
        }
        
        System.out.println("Size of data: " + vectors.size());

        return vectors;
        
    }

}

The last line " return vectors;" returns a list with the values i parsed and returns it like this: enter image description here

But i want this more "pretty", i want it to be one Array in each line, how can i achieve this?

P.S. Its on the .html page, not on console

Upvotes: 0

Views: 776

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18979

Your return value seems a valid Json Object. If you want it more pretty so you can read it clearly then pass it through an application that makes that json pretty.

If you call your API from Postman, it will give you a pretty Json Object which will be better formatted. This will be because you have annotated your controller with @RestController so it will deliver an application/json response which Postman will know and then it will try to make it prettier.

P.S. Its on the .html page, not on console

So you hit your API from a browser. Most browsers don't expect a Json object to be returned, so they will not make it pretty. You can't force that from your Service either.

Just hit your API from Postman, it will understand it and make it pretty.

Upvotes: 1

Related Questions