I use java 1.8 and spring boot 2.7.4.
I want to access a distant REST server and I'm stuck at the beginning.
I used this tutorial to write the code.
The compilation went fine but at the end of the execution no request was made and I don't understand why. I redirected java to a proxy and I can see no request is made, I should see an error regarding the missing certificate...
This is the code:
@SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
System.out.println("Start build request");
WebClient client = WebClient.create("https://test.caido.ro");
UriSpec<RequestBodySpec> uriSpec = client.post();
RequestBodySpec bodySpec = uriSpec.uri("/api/login");
RequestHeadersSpec<?> headersSpec = bodySpec.body(BodyInserters.fromValue("{\n"
" \"email\": \"test\",\n"
" \"password\": \"test\"\n"
"}"));
System.out.println("Start send request");
Mono<String> response = headersSpec.retrieve().bodyToMono(String.class);
System.out.println("Stop send request " response.toString());
}
}
CodePudding user response:
WebClient is not your only option. You can try some other Http clients that might be simpler in use. Here are a few options:
- Apache HttpClient - a very widely used library
- OK HttpClient - Open Source library
- And my favorite (Open Source library written by me) MgntUtils library
With MgntUtils library your code could be as simple as
private static void testHttpClient() {
HttpClient client = new HttpClient();
client.setContentType("application/json; charset=utf-8");
client.setConnectionUrl("http://www.your.url.com/");
String content = null;
try {
content = client.sendHttpRequest(HttpMethod.POST, "Some textual conent - like your JSON");
} catch (IOException e) {
content = TextUtils.getStacktrace(e, false);
}
System.out.println(content);
}
Here is Javadoc for MgntUtils HTTPClient class. The library itself could be found here as Maven artifacts or on Git (including sources and JavaDoc). An article about the library (although it doesn't describe HttpClient feature) could be found here
CodePudding user response:
Just did some debugging and found out that you've made several mistakes:
1, You're executing your application without the SpringApplication.run(WebClientApplication.class, args);
This means it will execute everything and then terminates before you can even receive the response (which will be done in another thread to achieve a-sync).
2, You're working with a Mono
, it's more like a Promise
in JS, it stands as a wrapper for the response when it arrives, after the response comes to can subscribe and do things with it, but before that, there's basically nothing inside this object, so toString()
won't get you the response you want.
Here's my suggestion:
1, Explicitly tell WebClient that you're waiting for your response, and you're not doing anything until you get that, by using Mono.block()
Like this :
System.out.println("Stop send request " response.block());
2, You have a subscriber to handle the response when it comes, like this :
response.subscribe(e -> {
System.out.println("------------------------------RESPONSE----------------------------------");
System.out.println(e);
System.out.println("------------------------------END----------------------------------");
});
But this lambda
will also execute in another thread, and may get terminated before execution if you main thread dies. So if you still want to test it in you current example, you must tell the main thread to wait for a while, using Thread.sleep()
And now you should see the response, or at least the request.