Java HttpClient-将HttpResponse转换为字符串[]
我是java HttpClient新手,目前正在尝试创建RestServiceApplication,但我无法将HttpResponse转换为字符串数组以访问数组元素Java HttpClient-将HttpResponse转换为字符串[],java,json,httpclient,httpresponse,java-http-client,Java,Json,Httpclient,Httpresponse,Java Http Client,我是java HttpClient新手,目前正在尝试创建RestServiceApplication,但我无法将HttpResponse转换为字符串数组以访问数组元素 package com.example.restservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBoot
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请求这些方法并访问数组的字符串
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);
}
}
我的问题是返回的字符串[]数组不是数组,而是单个字符串(BodyHandler.ofString()),因此我无法再访问元素。我希望用BodyHander.ofLine()方法解决这个问题,将响应转换为流,然后在流上调用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());
}
}
package com.example.restservice;
导入java.io.IOException;
导入java.net.URI;
导入java.net.http.HttpClient;
导入java.net.http.HttpRequest;
导入java.net.http.HttpResponse;
导入java.util.Scanner;
导入java.util.stream.stream;
公共类GetRequest{
公共静态void main(字符串[]args)引发IOException、InterruptedException{
HttpClient=HttpClient.newHttpClient();
/*
*阿伯弗雷格·伦克泰彭
*/
HttpRequestLenkerTypen=HttpRequest.newBuilder()
.uri(uri.create(“http://localhost:8081/getlenkertypen"))
.build();
HttpResponse lenkerResponse=client.send(requestLenkertypen,
HttpResponse.BodyHandlers.ofLines());
String[]lenkerArray=lenkerResponse.body().toArray(String[]::new);
System.out.println(“bite wählen Sie alstes den Lenker aus:”);
对于(int i=0;i
通过谷歌搜索,我发现我需要解析JSON响应,但我不知道如何解析。我从spring.io下载了项目结构,所以我认为响应返回为JSON,但我对这一点不熟悉,任何帮助都会得到通知
谢谢 如果希望一次获取一行响应,请使用BufferedReader和lines()方法。
然而,您真正想要的似乎是解析返回的JSON数据。为此,有许多可能性。考虑使用杰克逊:是的,你是对的,我真正想要的是解析JSON!谢谢我会看看杰克逊的