6

I am writing a simple get method to retrieve reviews from an API URL. The API is returning json data as String. Returning Mono<Object> throws an error. Please find below the HTTP response.

{
    "timestamp": "2019-02-05T11:25:33.510+0000",
    "path": "Some URL",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Content type 'text/plain;charset=utf-8' not supported for bodyType=java.lang.Object"
}

I found that the response is a string. So returning Mono<String> is working fine. But I want to return Mono<MyObject> from the API response.

How do I convert Mono<String> to Mono<MyObject>? I could not find any solutions on google except How to get String from Mono<String> in reactive java.

Below is my service class:

@Service
public class DealerRaterService {
    WebClient client = WebClient.create();
    String reviewBaseUrl = "Some URL";

    public Mono<Object> getReviews(String pageId, String accessToken) {
        String reviewUrl = reviewBaseUrl + pageId + "?accessToken=" + accessToken;
        return client.get().uri(reviewUrl).retrieve().bodyToMono(Object.class);
    }
}

Edit: Adding my controller class:

@RestController
@RequestMapping("/path1")
public class DealerRaterController {

    @Autowired
    DealerRaterService service;

    @RequestMapping("/path2")
    public Mono<Object> fetchReview(@RequestParam("pageid") String pageId,
            @RequestParam("accesstoken") String accessToken) throws ParseException {
        return service.getReviews(pageId, accessToken);
    }
}

Let me know you need any more information.

theone1one
  • 1,510
  • 3
  • 20
  • 27
  • From the message, it seems you have not set the proper response type. Can you provide your controller class here? – Akash Feb 05 '19 at 11:59
  • I have edited and added controller class to the description Akash. – theone1one Feb 05 '19 at 13:28
  • you want to return whihc kind of object , because in your question you mentienned that you use Object class – Bourbia Brahim Feb 05 '19 at 14:03
  • I will write my own POJO class but at first, converting the response to Object class is not working. So I am stuck here. – theone1one Feb 05 '19 at 16:20
  • @theone1one you need to write POJO class and return that. Returning plain Object generally doesn't work in Spring. When you are returning Mono, spring/json is able to find a converter and hence you are getting proper output. When you are returning Mono, it is not able to find any converter for Object (which is correct). Can you try creating a simple Pojo and return something like Mono and see if it works. I assume it will work as you expected then. – Akash Feb 06 '19 at 05:46
  • 1
    Make sure whatever you are getting from retrieve(), your POJO is aligned according to this. You may also get WebClientException while conversion if you are getting 4xx or 5xx from the APIs you are calling. So you might want to handle it as well. – Akash Feb 06 '19 at 05:55
  • @Akash I tried using myPojo. Still, that is not working. ```return client.get().uri(reviewUrl).retrieve().bodyToMono(DealerReview.class);``` Its throwing same error message ```Content type 'text/plain;charset=utf-8' not supported for bodyType=com.reputation.api.dealerrater.model.DealerReview``` – theone1one Feb 07 '19 at 06:43

1 Answers1

2

This is how I fixed my problem. Used map to retrieve string and converting that string to my POJO class using ObjectMapper class.

@Service
public class DealerRaterService {
    WebClient client = WebClient.create();
    String reviewBaseUrl = "some url";

    public Mono<DealerReview> getReviews(String pageId, String accessToken)
            throws JsonParseException, JsonMappingException, IOException {
        String reviewUrl = reviewBaseUrl + pageId + "?accessToken=" + accessToken;
        Mono<String> MonoOfDR = client.get().uri(reviewUrl).retrieve().bodyToMono(String.class);

        return MonoOfDR.map(dealerRater -> {
            try {
                DealerReview object = new ObjectMapper().readValue(dealerRater, DealerReview.class);
                return object;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        });

    }

}
theone1one
  • 1,510
  • 3
  • 20
  • 27