当我尝试将响应体反序列化为POJO时,当未使用POJO(ignoreUnknown= true)时,设置了空值,遇到以下错误: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“JsonIgnoreProperties”(类TestNGMaven.restAssuredProject.WeatherInfo),未标记为可忽略(6个已知属性:“湿度”、“温度”、"weatherDescription“、”城市“、"windSpeed”、"windDirectionDegree"])
下面是我使用的代码:
public class WeatherInfo
{
private String city;
private String temperature;
private String humidity;
private String weatherDescription;
private String windSpeed;
private String windDirectionDegree;
// getters and setters
}使用的方法:
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":“度数”}“
此外,将城市更改为城市并不能解决错误。
发布于 2018-10-14 01:32:17
通过在我使用GSON注释样式的POJO类中添加以下代码,解决了这个问题
@SerializedName("City")
@Expose
private String city;并使用以下格式进行反序列化
WeatherInfo weatherInfo =responseBody.as(WeatherInfo.class,ObjectMapperType.GSON);https://stackoverflow.com/questions/52795030
复制相似问题