0

How do I store the response body into String array?

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response
            = restTemplate.getForEntity("http://localhost:9100/metrics", String.class);

Here is my http response:

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.6951e-05
go_gc_duration_seconds{quantile="0.25"} 7.3898e-05
go_gc_duration_seconds{quantile="0.5"} 0.000106166
go_gc_duration_seconds{quantile="0.75"} 0.000113714
go_gc_duration_seconds{quantile="1"} 0.000744301
go_gc_duration_seconds_sum 0.001169959

I would like to store the above data in a String array so that I can remove the comments and process the data.

How do i convert the above response into a String array?

2 Answers 2

1

Your response structure is not of type String array. It provides you strings possibly in new lines. To capture the response in String array, you need to change the response structure. Also, change the following line:

restTemplate.getForEntity("http://localhost:9100/metrics", String[].class);

Alternatively, get the response as String and then parse it to eliminate comments. Tokenize data by line separator or the separator response data has and then store into a String array.

Sign up to request clarification or add additional context in comments.

Comments

0

List object = Arrays.asList(response.getBody().split("\r?\n"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.