首页 > 解决方案 > Java HttpClient - 将 HttpResponse 转换为 String[]

问题描述

我是 java HttpClient 的新手,目前正在尝试创建一个 RestServiceApplication,但我无法将 HttpResponse 转换为 String 数组以访问数组的元素。

package com.example.restservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RestServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestServiceApplication.class, args);
    }

}

我已经为应用程序实现了以下控制器,它具有不同的方法,每个方法都返回一个字符串数组。

package com.example.restservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigurationController {
    
    
    @GetMapping("/getlenkertypen")
    public String[] getLenkertypen() {
        String[] lenker = new String[3];
        lenker[0] = "Flatbarlenker";
        lenker[1] = "Rennradlenker";
        lenker[2] = "Bullhornlenker";
        return lenker;
    }
    
    @GetMapping("/getmaterial")
    public String[] getMaterial() {
        String[] material = new String[3];
        material[0] = "Aluminium";
        material[1] = "Stahl";
        material[2] = "Kunststoff";
        return material;
    }
    
    @GetMapping("/getschaltung")
    public String[] getSchaltung() {
        String[] schaltung = new String[3];
        schaltung[0] = "Kettenschaltung";
        schaltung[1] = "Nabenschaltung";
        schaltung[2] = "Tretlagerschaltung";
        return schaltung;
    }
    
    @GetMapping("/getgriff")
    public String[] getGriff() {
        String[] griff = new String[3];
        griff[0] = "Ledergriff";
        griff[1] = "Schaumstoffgriff";
        griff[2] = "Kunststoffgriff";
        return griff;
    }
    
    @GetMapping("/test")
    public String test() {
        String[] griff = new String[3];
        griff[0] = "Ledergriff";
        griff[1] = "Schaumstoffgriff";
        griff[2] = "Kunststoffgriff";
        return "test";
    }
}

现在我希望 HttpClient 请求这些方法并访问数组的字符串。

我的问题是返回的 String[] 数组不是数组而是单个字符串(BodyHandler.ofString()),所以我不能再访问这些元素了。我希望使用 BodyHander.ofLine() 方法来解决这个问题,将响应转换为 Stream,然后在流上调用 toArray() 函数。但它仍在创建一个只有一个元素的数组,其中包含我从 ofString() 方法获得的相同字符串。这是我的客户:

package com.example.restservice;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
import java.util.stream.Stream;

public class GetRequest {

    public static void main(String[] args) throws IOException, InterruptedException {

        HttpClient client = HttpClient.newHttpClient();
        
        /*
         * Abfrage Lenkertypen
         */
        
        HttpRequest requestLenkertypen = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8081/getlenkertypen"))
                .build();
        
        HttpResponse<Stream<String>> lenkerResponse = client.send(requestLenkertypen,
                HttpResponse.BodyHandlers.ofLines());
        String[] lenkerArray = lenkerResponse.body().toArray(String[]::new);
        System.out.println("Bitte wählen Sie als erstes den Lenker aus: ");
        for (int i = 0; i < lenkerArray.length; i++) {
            System.out.println(lenkerArray[i] + " " + i);
        }
        System.out.println(lenkerArray[0]);
        Scanner scanner = new Scanner(System.in);
        
        int answer = scanner.nextInt();
        System.out.println(answer);
        
        
        HttpRequest requestSchaltung = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8081/getschaltung"))
                .build();

        HttpResponse<String> response = client.send(requestSchaltung,
                HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
    }
}

通过谷歌搜索,我发现我需要以某种方式解析响应 JSON,但我不知道如何。我从 spring.io 下载了项目结构,所以我认为响应返回为 JSON,但我对此并不陌生,任何帮助都会受到重视。

谢谢!

标签: javajsonhttpclienthttpresponsejava-http-client

解决方案


如果您想一次获取响应的行,请使用 BufferedReader 和 lines() 方法。但是,您似乎真正想要的是解析返回的 JSON 数据。为此,有很多可能性。考虑使用杰克逊: https ://www.baeldung.com/jackson


推荐阅读