0

I am new to java HttpClient and am currently trying to create a RestServiceApplication, but i am unable to convert a HttpResponse to a String array in order to access the elements of the array.

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);
    }

}

I have implemented the following controller for the application which has different methods that each return an array of String.

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";
    }
}

Now i want the HttpClient to request those methods and access the Strings of the array.

My problem is that the returned String[] arrays are not arrays but a single String (BodyHandler.ofString()), so i cannot access the elements anymore. I hoped to solve this with the BodyHander.ofLine()-method, converting the response to a Stream and then call the toArray() function on the stream. But it is still creating an array that has only one element, containing the same String i get from the ofString()-method. Here is my client:

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());
    }
}

By googling i found out that i somehow need to parse the response JSON, but i do not know how. I downloaded the project structure from spring.io so i think the response returns as JSON, but i am new to this and any help would be appreaciated.

Thanks!

daniel
  • 2,665
  • 1
  • 8
  • 18
Kev
  • 3
  • 4

1 Answers1

0

If you want to get the lines of the response one at a time use a BufferedReader and the lines() method. However, it seems what you really want is to parse the JSON data that is returned. For that there are many possibilities. Consider using Jackson: https://www.baeldung.com/jackson

swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • Yes you are right what i really want is to parse the JSON! Thank you i will have a look at jackson – Kev Nov 05 '20 at 18:50