Reputation: 1159
I am trying to make REST calls to the Yelp API
to get business data from my Java Spring MVC Web Application
. I am able to make the API call using Postman App
. Now I am trying to use the Jersey REST client. My REST
call will be as follows:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource("https://api.yelp.com/v3/businesses/{id}");
Object responseMsg = webResource
.header("Authorization", "Bearer My_Key")
.getClass();
According the Yelp documentation Yelp Documentation, an object will be returned if I make the API call. Is it not possible to store the returned data into a Java Oject
and get the required data from that Object.
Upvotes: 0
Views: 135
Reputation: 209082
You aren't even making a request. You need to use one of the methods on the WebResource.Builder
. Once you call header()
on the WebResource
, you get back a WebResource.Builder
. To make the API call, you need to use a method like get()
, post()
, put()
, etc. Pass ClientResponse.class
as the argument so you get back a ClientResponse
. The you can check the status on the ClientResponse
. If the status is ok, then call response.getEntity(POJO.class)
, where POJO.class
is your own model class representation of the Yelp JSON data. Something like
ClientResponse response = webResource
.header("Authorization", "Bearer My_Key")
.get(ClientResponse.class);
if (response.getStatus() == 200) {
YelpModel model = response.getEntity(YelpModel.class);
}
You need to make sure you have the jersey-json
dependency and configure the JSON POJO feature with the client
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
YelpModel
is your custom POJO class that models the Yelp JSON. If you don't know how to make this class, then you might need to go over some Jackson tutorials on how to map JSON to Java objects.
Upvotes: 0