How to get the JSON of a response from a service

BugsForBreakfast :

Hey guys im trying to get a response from this service (http://ip-api.com) that gives you latitude and longitude based on an ip:

So when you pass the ip 55.130.54.69 it returns the following json:

{
    "query": "55.130.54.69",
    "status": "success",
    "continent": "North America",
    "continentCode": "NA",
    "country": "United States",
    "countryCode": "US",
    "region": "AZ",
    "regionName": "Arizona",
    "city": "Sierra Vista",
    "district": "Fort Huachuca",
    "zip": "85613",
    "lat": 31.5552,
    "lon": -110.35,
    "timezone": "America/Phoenix",
    "currency": "USD",
    "isp": "CONUS-RCAS",
    "org": "USAISC",
    "as": "AS721 DoD Network Information Center",
    "asname": "DNIC-ASBLK-00721-00726",
    "mobile": false,
    "proxy": false
}

http://ip-api.com/#55.130.54.69

So in my service im doing the following (I guided with this Best way to get geo-location in Java):

    @POST
    @Path("/test2")
    public void test2(@Context HttpServletRequest request) {

        String ip = request.getRemoteAddr();
        System.out.println("ip: " + ip);
        //Im changing value of ip cause I have an issue with "private range" ip of my machine
        ip = "55.130.54.69";
        // This is working
        Client client = ClientBuilder.newClient();
        Response response = client.target("http://ip-api.com/json/" + ip).request(MediaType.TEXT_PLAIN_TYPE)
                .header("Accept", "application/json").get();

        System.out.println("status: " + response.getStatus()); // Printing 200 so it worked
        System.out.println("body:" + response.getEntity());
        System.out.println("metadata: " + response.getMetadata());
        System.out.println(response);
    }

So as you can see im trying to get that json above in my question but I don't know how, can you show me the way please?

Ken Bekov :

If you need to get json as a plain text, you can try the next:

@POST
@Path("/test2")
public void test2(@Context HttpServletRequest request) {

    ...

    Response response = client.target("http://ip-api.com/json/" + ip)
        .request(MediaType.TEXT_PLAIN_TYPE)
        .header("Accept", "application/json").get();

   String json = response.readEntity(String.class);
   response.close();

   // now you can do with json whatever you want to do
}

Also you can create an entity class where field names match value names in json:

public class Geolocation {
    private String query;
    private String status;
    private String continent;

    // ... rest of fields and their getters and setters      
}

Then you can read data as an instance of the entity:

@POST
@Path("/test2")
public void test2(@Context HttpServletRequest request) {

    ...

    Response response = client.target("http://ip-api.com/json/" + ip)
        .request(MediaType.TEXT_PLAIN_TYPE)
        .header("Accept", "application/json").get();

   Geolocation location = response.readEntity(Geolocation.class);
   response.close();

   // now the instance of Geolocation contains all data from the message
}

If you're not interested in getting details of response you cant get result message right from the get() method:

Geolocation location = client.target("http://ip-api.com/json/" + ip)
    .request(MediaType.TEXT_PLAIN_TYPE)
    .header("Accept", "application/json").get(Geolocation.class);

// just the same has to work for String

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145676&siteId=1