2014-02-12 15 views
0

我目前正在嘗試從該站點檢索信息:http://freegeoip.net/json/184.71.175.150,並使用Jersey將其內容讀入我自己的自定義對象中。使用Jersey自定義JSON與傑克遜

從這個鏈接https://jersey.java.net/documentation/latest/user-guide.html#json.jackson,章8.1.4,我寫了下面的代碼:

final Client client = ClientBuilder.newBuilder().register(IPInfo.class).register(JacksonFeature.class).build(); 

     Response response = client.target("http://freegeoip.net/json/184.71.175.150").request(MediaType.APPLICATION_JSON).get(); 
     IPInfo ip = response.readEntity(IPInfo.class); 

問題是,在response.readEntity線,將「HTTP狀態500代碼遊 - 從錯誤讀取實體輸入流「顯示在我的servlet上。

這是我的對象類:

public class IPInfo 
{ 

private String _ip; 
private String _countryCode; 
private String _countryName; 
private String _regionCode; 
private String _regionName; 
private String _city; 
private String _zipCode; 
private float _latitude; 
private float _longitude; 
private String _metroCode; 
private String _areaCode; 

public IPInfo(String ip, 
       String countryCode, 
       String countryName, 
       String regionCode, 
       String regionName, 
       String city, 
       String zipCode, 
       float latitude, 
       float longitude, 
       String metroCode, 
       String areaCode) 
{ 
    _ip = ip; 
    _countryCode = countryCode; 
    _countryName = countryName; 
    _regionCode = regionCode; 
    _city = city; 
    _zipCode = zipCode; 
    _latitude = latitude; 
    _longitude = longitude; 
    _metroCode = metroCode; 
    _areaCode = areaCode; 
    _regionName = regionName; 
} 

public String getIp() 
{ 
    return _ip; 
} 

public String getCountryCode() 
{ 
    return _countryCode; 
} 

public String getCountryName() 
{ 
    return _countryName; 
} 

public String getRegionCode() 
{ 
    return _regionCode; 
} 

public String getCity() 
{ 
    return _city; 
} 

public String getZipCode() 
{ 
    return _zipCode; 
} 

public float getLatitude() 
{ 
    return _latitude; 
} 

public float getLongitude() 
{ 
    return _longitude; 
} 

public String getMetroCode() 
{ 
    return _metroCode; 
} 

public String getAreaCode() 
{ 
    return _areaCode; 
} 

public String getRegionName() 
{ 
    return _regionName; 
} 

public void setIp(String ip) 
{ 
    _ip = _ip; 
} 

public void setCountryCode(String countryCode) 
{ 
    _countryCode = countryCode; 
} 

public void setCountryName(String countryName) 
{ 
    _countryName = countryName; 
} 

public void setRegionCode(String regionCode) 
{ 
    _regionCode = regionCode; 
} 

public void setRegionName(String regionName) 
{ 
    _regionName = regionName; 
} 

public void setCity(String city) 
{ 
    _city = city; 
} 

public void setZipCode(String zipCode) 
{ 
    _zipCode = zipCode; 
} 

public void setLatitude(float latitude) 
{ 
    _latitude = latitude; 
} 

public void setLongitude(float longitude) 
{ 
    _longitude = longitude; 
} 

public void setMetroCode(String metroCode) 
{ 
    _metroCode = metroCode; 
} 

public void setAreaCode(String areaCode) 
{ 
    _areaCode = areaCode; 
} 

} 

任何幫助,將不勝感激,謝謝

回答

0

嘗試獲得響應作爲字符串第一。確保您得到的響應實際上是JSON,並且它的格式與對象相同。

ClientResponse resp = service.path(PATH) 
      .type(MediaType.APPLICATION_JSON).get(ClientResponse.class); 

resp.getEntity(String.class) 

然後你就可以用ObjectMapper 最終ObjectMapper映射器=新ObjectMapper測試反串行化代碼(); mapper.readValue(resp.getEntity(String.class),CLASS)

+0

你可以有機會您的字段的名稱與JSON註釋@JsonProperty(「Name」) – pwilmot

0

我已取回從網站的信息:http://freegeoip.net/json/184.71.175.150,並閱讀了您的自定義對象IPInfo內容使用新澤西州。

這裏是我的代碼來測試解析:

public static void main(String[] args) throws Exception { 
    Client client = Client.create(); 
    WebResource webResource = client.resource("http://freegeoip.net/json/184.71.175.150"); 
    ClientResponse response = webResource.accept("application/json") 
             .get(ClientResponse.class); 
    // Object to parser VO's 
    ObjectMapper objectMapper = new ObjectMapper(); 
    objectMapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
    IPInfo ipInfo = objectMapper.readValue(response.getEntityInputStream(), IPInfo.class); 
} 

注:

新澤西1.x的方式:

Client client = Client.create(); 
WebResource webResource = client.resource(restURL).path("myresource/{param}"); 
String result = webResource.pathParam("param", "value").get(String.class); 

JAX-RS 2.0的方式:

Client client = ClientBuilder.newClient(); 
WebTarget target = client.target(restURL).path("myresource/{param}"); 
String result = target.pathParam("param", "value").get(String.class); 

這是IPInfo類(通過添加空的構造函數,更改「setIp」方法並進行格式化來修改。如果你不喜歡這種方式格式化的課堂上,我可以撤消它:P):

public class IPInfo { 

    private String _ip; 
    private String _countryCode; 
    private String _countryName; 
    private String _regionCode; 
    private String _regionName; 
    private String _city; 
    private String _zipCode; 
    private float _latitude; 
    private float _longitude; 
    private String _metroCode; 
    private String _areaCode; 

    public IPInfo() { 

    } 

    public IPInfo(String ip, String countryCode, String countryName, 
      String regionCode, String regionName, String city, String zipCode, 
      float latitude, float longitude, String metroCode, String areaCode) { 
     _ip = ip; 
     _countryCode = countryCode; 
     _countryName = countryName; 
     _regionCode = regionCode; 
     _city = city; 
     _zipCode = zipCode; 
     _latitude = latitude; 
     _longitude = longitude; 
     _metroCode = metroCode; 
     _areaCode = areaCode; 
     _regionName = regionName; 
    } 

    public String getIp() { 
     return _ip; 
    } 

    public String getCountryCode() { 
     return _countryCode; 
    } 

    public String getCountryName() { 
     return _countryName; 
    } 

    public String getRegionCode() { 
     return _regionCode; 
    } 

    public String getCity() { 
     return _city; 
    } 

    public String getZipCode() { 
     return _zipCode; 
    } 

    public float getLatitude() { 
     return _latitude; 
    } 

    public float getLongitude() { 
     return _longitude; 
    } 

    public String getMetroCode() { 
     return _metroCode; 
    } 

    public String getAreaCode() { 
     return _areaCode; 
    } 

    public String getRegionName() { 
     return _regionName; 
    } 

    public void setIp(String ip) { 
     _ip = ip; 
    } 

    public void setCountryCode(String countryCode) { 
     _countryCode = countryCode; 
    } 

    public void setCountryName(String countryName) { 
     _countryName = countryName; 
    } 

    public void setRegionCode(String regionCode) { 
     _regionCode = regionCode; 
    } 

    public void setRegionName(String regionName) { 
     _regionName = regionName; 
    } 

    public void setCity(String city) { 
     _city = city; 
    } 

    public void setZipCode(String zipCode) { 
     _zipCode = zipCode; 
    } 

    public void setLatitude(float latitude) { 
     _latitude = latitude; 
    } 

    public void setLongitude(float longitude) { 
     _longitude = longitude; 
    } 

    public void setMetroCode(String metroCode) { 
     _metroCode = metroCode; 
    } 

    public void setAreaCode(String areaCode) { 
     _areaCode = areaCode; 
    } 

} 

注意:您將需要@JsonProperty(「姓名」)添加爲「pwilmot」說,爲了用「_」作爲「region_code」獲取含有化合物名稱的數據,或稍微修改你的類。

我沒有這樣做,因爲我想告訴你,與你的類(無註釋)是可能的解析器,並獲得來自URI數據到IPInfo