Home > Software engineering >  consuming get API using webclient
consuming get API using webclient

Time:06-22

My API is returning below response when I am calling from postman. I am not getting proper response from webclient. I am doing something wrong. I am quite new.

{
    "loginAccounts": [
        {
            "name": "Oracle",
            "accountId": "16485679",
            "baseUrl": "https://demo.docusign.net/restapi/v2.1/accounts/16485679",
            "isDefault": "true",
            "userName": "Mahima Pandey",
            "userId": "e48dbc73-97df-42de-be9f-ea49c8b24712",
            "email": "[email protected]",
            "siteDescription": ""
        }
    ]
}

when I am consuming using webclient.

public static Flux<LoginAccounts> retrieveDetails(){
        Flux<LoginAccounts> loginAccounts=null;
        WebClient webclient = WebClient.create();
        try {
        loginAccounts = webclient.get().uri(DocusignConstants.LOGIN_INFO_URL)
                .header("Accept", "application/json")
                .header("Authorization", "Bearer eyJ0eX...........")
                .retrieve()
                .bodyToFlux(LoginAccounts.class);
        System.out.println(loginAccounts);
        }
        catch(WebClientResponseException ex) {
            System.out.println(ex);
        }
        return loginAccounts;
    }

it is simple returing MonoFlatMapMany .how can I get proper response?

my POJO is below.

@data
@Noargsconstructor
@Allargsconstructor
public class LoginAccounts {

    private String name;
    private String accountId;
    private String baseUrl;
    private Boolean isDefault;
    private String userName;
    private String userId;
    private String email;
    private String siteDescription;

}

if I write this line System.out.println(loginAccounts);it prints

MonoFlatMapMany

but if i use stram it starts giving exception

loginAccounts.toStream().forEach(data -> System.out.println(data));

Below is the exception.

org.springframework.web.reactive.function.client.WebClientRequestException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
2022-06-21 11:09:50.494  WARN 20024 --- [ctor-http-nio-2] r.netty.http.client.HttpClientConnect    : [099818dc, L:/172.20.10.3:58444 - R:demo.docusign.net/162.248.186.25:443] The connection observed an error

javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131) ~[na:na]
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:371) ~[na:na]
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:314) ~[na:na]
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:309) ~[na:na]
    at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.checkServerCerts(CertificateMessage.java:1357) ~[na:na]
    at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.onConsumeCertificate(CertificateMessage.java:1232) ~[na:na]
    at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.consume(CertificateMessage.java:1175) ~[na:na]
    at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:396) ~[na:na]
    at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:480) ~[na:na]
    at java.base/sun.security.ssl.SSLEngineImpl$DelegatedTask$DelegatedAction.run(SSLEngineImpl.java:1277) ~[na:na]
    at java.base/sun.security.ssl.SSLEngineImpl$DelegatedTask$DelegatedAction.run(SSLEngineImpl.java:1264) ~[na:na]
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:712) ~[na:na]
    at java.base/sun.security.ssl.SSLEngineImpl$DelegatedTask.run(SSLEngineImpl.java:1209) ~[na:na]

from postman I am getting proper response.

enter image description here

CodePudding user response:

Instead of using System.out.println for printing the output. You can use the looping to print the values.

Try with the below code:

loginAccounts.toStream().forEach(data -> System.out.println(data));

CodePudding user response:

At fist you should solve your problem with SSL. So import your SSL cert to your certstore (take a inspiration here).

Second thing is that if you are working with Flux it doesn't mean that the response is available immediately. You should get it somehow. You could do something like

List<LoginAccounts> accounts = loginAccounts.collectList().block();
  • Related