【问题标题】:How to format http response into string array using spring rest template?如何使用spring rest模板将http响应格式化为字符串数组?
【发布时间】:2019-08-06 18:55:10
【问题描述】:

如何将响应正文存储到字符串数组中?

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

这是我的 http 响应:

# 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

我想将上述数据存储在一个字符串数组中,以便我可以删除 cmets 并处理数据。

如何将上述响应转换为字符串数组?

【问题讨论】:

    标签: spring-boot resttemplate


    【解决方案1】:

    您的响应结构不是String 数组类型。它可能为您提供新行中的字符串。要捕获String 数组中的响应,您需要更改响应结构。另外,更改以下行:

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

    或者,将响应作为字符串获取,然后对其进行解析以消除 cmets。通过line separatorseparator 响应数据标记数据,然后存储到String 数组中。

    【讨论】:

      【解决方案2】:

      列表对象 = Arrays.asList(response.getBody().split("\r?\n"));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-10
        • 2023-04-06
        • 1970-01-01
        • 2020-02-02
        相关资源
        最近更新 更多