0

I am working on a project which until now has been using the org.springframework.web.client.RestTemplate I have recently understood that this class is to be deprecated in favour of the Asynch org.springframework.web.reactive.function.client.WebClient framework. Now I am massively in favour of this, as my application is suffering from long delays on waiting from responses from RestTemplate (GET) calls (in which time I could be doing database stuff etc.).
The problem that I have now is that if I make a call like:

final Mono<String> call = webClient
        .get()
        .uri("/base/recordPath/1?format=json")
        .header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
        .accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .bodyToMono(String.class)

when I make a subsequent call like:
System.out.println(call.block());
I get the expected output (a String version of a populated Json Object).
however if I change the earlier call to (which I want to do!):

final Mono<JsonObject> call = webClient
        .get()
        .uri("/base/recordPath/1?format=json")
        .header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
        .accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .bodyToMono(JsonObject.class)

when I do:
System.out.println(call.block());
I just get {} instead of a populated JsonObject

So it looks like the bodyToMono() hasn't done what 'I' expected

When I used RestTemplate, there was a way to register serialisers with the Template (though this wasn't necessary for JsonObject), is this also necessary with WebClient? If so how do you do it?
I would be grateful of help.

Thanks

Bill

N.B. I'm not sure if this has any relevance, but the rest endpoint I am accessing does have an IP restriction, so if in some way the WebClient were to alter the originating IP, this may have some effect. Though I would have thought it would be more like a 4** of some sort, I'm not seeing any of those! Or possibly a Media type issue, as it is going through a DMZ, where the guardian may be changing an 'unauthorised' request from application/json to text/* for example.

Another point which may have relevance is that to get a successful start of the application it is necessary to run it with the following property: spring.main.web-application-type=none

Update
I now have my application running, though not as I want!
The issue appeared to be transitive dependencies imported by the team pom I am required to use (as a parent pom). I now get a successful start of the project. But still find that the json Object (which is now Jackson) is still Empty (as reported by object.isEmpty()).
my dependencies/versions now are:
org.springframework:5.2.8.RELEASE
org.springframework.boot:2.3.3.RELEASE
com.fasterxml.jackson:2.11.2
I know I am fighting a parent pom which is against what am trying to do but would like to know what the dependencies I really need are

Bill Naylor
  • 482
  • 1
  • 9
  • 14
  • 1
    `RestTemplate` isn't deprecate its async twin `AsyncRestTemplate` is deprecated in favor of `WebClient`. Also you are aware that when using `block()` you are basically loosing the advantages of `WebClient`? You are now in a blocking state again. That being said, Which `JsonObject` do you use (which package). WebClient only has support for Jackson, so if this is the GSON one it won't work. – M. Deinum Aug 20 '20 at 09:47
  • I was aware, that using block was loosing the advantages of WebClient, it was a stepping stone to a more complete solution. I was also using the GSON JsonObject, so it looks like this may be the issue. If you could write this up as an answer, together with a link to some documentation which states that Jackson is the only Json supported I will take this as the excepted answer. Thanks. – Bill Naylor Aug 20 '20 at 10:09
  • also `org.json` isn't Jackson nor GSON. So please make this explicit in your question. – M. Deinum Aug 24 '20 at 05:17
  • I was not aware that I was using `org.json`, though `mvn dependency:tree` tells me that `com.fasterxml.jackson.datatype:jackson-datatype-json-org:jar:2.11.2` is pulling in `org.json:json:jar:20190722:compile`, doesn't that imply that `org.json` is part of jackson? Or am I missing something? – Bill Naylor Aug 24 '20 at 08:03
  • It isn't part of Jackson. That dependency is pulling in an additional dependency making Jackson aware of the `org.json` stuff and allowing conversion for it. Which makes me wonder then when you switch to `WebClient` it stops working. Do you change dependencies for working with the `WebClient`? Try to compare the before and after dependencies and see what the differences are. Please also add your `pom.xml` to your question. – M. Deinum Aug 24 '20 at 08:31
  • I believe that the information given by @M.Deinum has been useful. I had thought `JSONObject` was org.json, thanks for the correction. I tried changing the body of the Mono to `ObjectNode` it was resolved successfully. However the body of my own Objects still were not being resolved. I believe this is due to the parent pom not being new enough. So my new approach will be to look at updating the parent pom. – Bill Naylor Aug 29 '20 at 15:21
  • I think this questions should be closed. I understand that it is bad practice to close your own questions, should a moderator validate this? – Bill Naylor Aug 29 '20 at 20:28

0 Answers0