Java 如何从服务获取响应的JSON

Java 如何从服务获取响应的JSON,java,json,rest,Java,Json,Rest,嘿,伙计们,我正在尝试从该服务()获取响应,该服务根据ip为您提供纬度和经度: 因此,当您通过ip 55.130.54.69时,它将返回以下json: { "query": "55.130.54.69", "status": "success", "continent": "North America", "continentCode": "NA", "country": "United States", "countryCode": "US",

嘿,伙计们,我正在尝试从该服务()获取响应,该服务根据ip为您提供纬度和经度:

因此,当您通过ip 55.130.54.69时,它将返回以下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
}

因此,在我的服务中,我做了以下几件事(我在这方面做了指导):


正如你在我的问题中看到的,我正试图获取上面的json,但我不知道如何获取,你能告诉我方法吗?

如果你需要以纯文本形式获取json,你可以尝试下一种方法:

@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
}
您还可以创建一个实体类,其中字段名与json中的值名匹配:

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

    // ... rest of fields and their getters and setters      
}
然后,您可以将数据作为实体的实例读取:

@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
}
如果您对获取响应的详细信息不感兴趣,则无法直接从
get()
方法获取结果消息:

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

这印的是什么<代码>System.out.println(“body:+response.getEntity())
另外,您使用哪些库发布?是jersey吗?

嘿,看起来不错,但是我的响应变量没有readyEntity方法:(知道为什么吗?@BugsForBreakfast您能检查一下您的响应类的全名是否是
javax.ws.rs.core.response
?是的,它是man,您认为它是版本吗?导入来自哪里?我会检查哪个依赖项version@BugsForBreakfast这取决于您使用的Servlet应用服务器的类型。是Tomcat还是其他什么?@BugsForBreakfast我更新了答案。你有
get(Class responseType)
方法吗?你能调用它而不是
get()吗
?它打印正文:org.jboss.resteasy.client.jaxrs.internal.ClientResponse$InputStreamWrapper@160a4908try这:
BufferedReader in=new BufferedReader(new InputStreamReader(response.getEntity()));String inputLine;StringBuffer content=new StringBuffer();while((inputLine=in.readLine())!=null){content.append(inputLine);}in.close();
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