Home > other >  How to return New Dto if Web Client response is not successful (400,404 status)
How to return New Dto if Web Client response is not successful (400,404 status)

Time:04-18

     @Service
 public HumoGetList getHumoCardsByNumber() throws SSLException {

        HumoGetList responseSpec = client
                .post()
                .uri("https://192.168.100.133/v2/mb/customers")
                .headers(headers -> {
                    headers.setBearerAuth("OnlyForTestToken");
                    headers.setContentType(MediaType.APPLICATION_JSON);
                })
                .bodyValue("{\n"  
                        "\"params\": {\n"  
                        "\"phone\": \"" 99895655988 "\",\n"  
                        "\"userId\": \"1111111111\"\n"  
                        "}\n"  
                        "}")
                .retrieve()
                .bodyToMono(HumoGetList.class)
                .doOnError(s -> System.err.println(s))
                .doOnNext(s -> System.err.println(s)).block();


        return responseSpec;
    }

https://192.168.100.133/api/customers if value in Json is has in database This uri response, values. For example if i send 4367415, i get respsonse with 200 status

{
    "id": "123456789",
    "result": {
        "Customer": [
            {
                "customer": "124124",
                "bankName": "OneLoveBank",
                "cardholder": "John Smith",
             }
]
}
}

And my service return Object with values. It is true working, but If my request's answer not found or may be my value is wrong (less then 6 symbols), i take response with 400 and 404 status.

But i want, if my request is wrong maybe not founded, i need return new HumoGetList Dto (with null values) in my Service.

CodePudding user response:

   .retrieve()
                .bodyToMono(HumoGetList.class)
                .onErrorReturn(new HumoGetList())
               .block();
  • Related