0

I am using a RestTemplate to consume a rest service that returning multiple lines of Json object using postman. But it returns only the first element.

I activated the log using : logging.level.org.apache.http=DEBUG Then I found all lines :

2020-02-06 18:49:17.046 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "b3[\r][\n]"
2020-02-06 18:49:17.046 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "{"targetOrder":0,"populationId":"71","to":1003319,"personIid":"a8a828ee89f6fd41d88fdd6bc7714779b0c5d4db4fcc6414d35e5801fad06aaa","from":1003319,"coverId":96,"type":"LOCAL_READY"}[\n]"
2020-02-06 18:49:17.046 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "[\r][\n]"
2020-02-06 18:49:17.060 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "b3[\r][\n]"
2020-02-06 18:49:17.061 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "{"targetOrder":0,"populationId":"71","to":1006569,"personIid":"02f1d37de11424a4dd558db2c10cb0159b5f379a843ca8646226ba8b59998bb0","from":1006569,"coverId":96,"type":"LOCAL_READY"}[\n]"
2020-02-06 18:49:17.061 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "[\r][\n]"
2020-02-06 18:49:17.061 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "b3[\r][\n]"
2020-02-06 18:49:17.061 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "{"targetOrder":0,"populationId":"71","to":1009703,"personIid":"5dc4eef92350c93cba70c02efbf289067a33ef231c87c610bf74daa51232d310","from":1009540,"coverId":96,"type":"LOCAL_READY"}[\n]"
2020-02-06 18:49:17.061 DEBUG 84151 --- [nio-8080-exec-1] org.apache.http.wire                     : http-outgoing-0 << "[\r][\n]"

But RestTemplate takes only the first line => ' "to":1003319'

Here my code :

    @GetMapping("/person/{id}")
public Object getPersonCover(@PathVariable(value ="id") int id) throws IOException {


    HttpHeaders headers = new HttpHeaders();
    headers.set("x-token", SECRET_TOKEN");
    HttpEntity entity = new HttpEntity(headers);

    ResponseEntity<Object> response = restTemplate.exchange(
            personCoverURL+id, HttpMethod.GET, entity,Object.class);
    return response.getBody();// returns only the first line :( 
}

I am using SpringBoot v2.1.4.RELEASE

Matthias
  • 1,378
  • 10
  • 23
Aguid
  • 943
  • 2
  • 10
  • 24
  • At first you only parse to an Object instead an List. For List use Object[].class or `ParameterizedTypeReference> myBean = new ParameterizedTypeReference>() {};` What is the Content-Type of this response? For me it looks not Http/JSON conform. – Matthias Feb 06 '20 at 18:22
  • The content-type is application/json. Using throws the following exception "com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token" – Aguid Feb 06 '20 at 19:26
  • Yes, because JSON Lists must start with [ and ends with ]. Your Response look like application/stream+json – Matthias Feb 06 '20 at 19:29
  • I think so .. should I use WebClient ? Reactive Spring ? – Aguid Feb 06 '20 at 19:30
  • Spring Reactive WebClient would handle this as Flux, yes. But it is not easy to migrate Spring MVC to Spring Webflux. – Matthias Feb 06 '20 at 19:44
  • It is also possible to use a WebSocket client to receive stream data. Maybe this will help https://stackoverflow.com/questions/48793167/connecting-and-sending-message-between-spring-websocket-instances – Matthias Feb 06 '20 at 19:46
  • Thanks @Matthias for your help, finally I resolved using WebClient of Reactive Spring :) – Aguid Feb 06 '20 at 20:00

1 Answers1

0

I used WebClient to retrieve the content which is json stream.

Here the new code :

 @GetMapping("/person/{id}")
public Flux<PersonCover> getPersonCover(@PathVariable(value = "id") int id) throws IOException {
    return webClient.get()
            .uri("population/{id}/person/cover", id)
            .headers(httpHeaders -> httpHeaders.set("x-token", "SECRET_TOKEN"))
            .accept(MediaType.APPLICATION_JSON)

            .retrieve()
            .bodyToFlux(PersonCover.class);
}
Aguid
  • 943
  • 2
  • 10
  • 24