首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用restassured对ResponseBody进行反序列化

未使用restassured对ResponseBody进行反序列化
EN

Stack Overflow用户
提问于 2018-10-14 00:34:00
回答 1查看 407关注 0票数 0

当我尝试将响应体反序列化为POJO时,当未使用POJO(ignoreUnknown= true)时,设置了空值,遇到以下错误: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“JsonIgnoreProperties”(类TestNGMaven.restAssuredProject.WeatherInfo),未标记为可忽略(6个已知属性:“湿度”、“温度”、"weatherDescription“、”城市“、"windSpeed”、"windDirectionDegree"])

下面是我使用的代码:

代码语言:javascript
复制
    public class WeatherInfo
{
    private String city;
    private String temperature;
    private String humidity;
    private String weatherDescription;
    private String windSpeed;
    private String windDirectionDegree;
    // getters and setters
}

使用的方法:

代码语言:javascript
复制
public void getWeatherDetailsForCity(String city) {

        RestAssured.baseURI="http://restapi.demoqa.com/utilities/weather/city";

        Response response= given().
                when().
                get("/"+city)
                .then()
                .extract()
                .response();

        ResponseBody responseBody=response.body();
        //No issues in below code
        System.out.println(responseBody.asString());
        //Exception for the below lines
        WeatherInfo weatherInfo =responseBody.as(WeatherInfo.class);    
        System.out.println(weatherInfo.getCity());

错误: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“城市”(类TestNGMaven.restAssuredProject.WeatherInfo),在[来源:(字符串)“{”城市“:”海得拉巴“,”温度“:"28.5摄氏度”,“湿度”:“62%”处未标记为可忽略(6个已知属性:“湿度”,“温度”,"weatherDescription",“城市”,"windSpeed","windDirectionDegree"]),"WeatherDescription":“雾霾”,"WindSpeed":“每小时1.5公里”,"WindDirectionDegree":“度数”}“

此外,将城市更改为城市并不能解决错误。

EN

回答 1

Stack Overflow用户

发布于 2018-10-14 01:32:17

通过在我使用GSON注释样式的POJO类中添加以下代码,解决了这个问题

代码语言:javascript
复制
@SerializedName("City")
    @Expose
    private String city;

并使用以下格式进行反序列化

代码语言:javascript
复制
WeatherInfo weatherInfo =responseBody.as(WeatherInfo.class,ObjectMapperType.GSON);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52795030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档