使用Jersey为Jackson定制JSON

时间:2014-02-12 17:57:38

标签: json object jersey jackson customization

我目前正在尝试从此网站检索信息:http://freegeoip.net/json/184.71.175.150,并使用Jersey将其内容读入我自己的自定义对象。

从这个链接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 Status 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;
}

}

感谢任何帮助,谢谢

2 个答案:

答案 0 :(得分:0)

首先尝试将响应作为String。确保您获得的响应实际上是JSON,并且其格式与对象相同。

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

resp.getEntity(String.class)

然后,您可以使用ObjectMapper测试反序列化代码     final ObjectMapper mapper = new ObjectMapper();     mapper.readValue(resp.getEntity(String.class),CLASS)

答案 1 :(得分:0)

我已从网站http://freegeoip.net/json/184.71.175.150检索到信息,并使用Jersey在自定义对象 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);
}

注意:

Jersey 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(“Name”)添加为“pwilmot”,以获取化合物名称为“_”作为“region_code”的数据,或稍微修改您的类。

我没有这样做,因为我想告诉你,你的类(没有注释)可以解析并从URI获取数据到IPInfo