【问题标题】:RestAPI post call setters and getters IssueRest API post call setter 和 getter 问题
【发布时间】:2021-12-29 16:42:57
【问题描述】:

我需要发送有效载荷,下面是详细信息。我不确定错误是由于传递了空数组还是什么原因。请帮忙。感谢期待。 PS:出于安全考虑,我不得不手动输入密码。

SumAll sa = new SumAll();
sa.setAge(68);
int[] myTrueAge ={};
sa.setTrueAge(myTrueAge);
During req it should go as -> "TrueAge":[],)
SumAll gsa = given().header(----).body(sa).expect().defaultParser(Parser.JSON).when().post(----).as(sumAll.class)

Pojo 类:

public class SumAll{
private int Age;
private int[] TrueAge= {};

public int[] getTrueAge(){
return TrueAge;

public void setTrueAge(int[] TrueAge)
TrueAge = TrueAge;
}}

错误:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognised field "Allocation" (class pojo.SumAll), not marked as ignorable (14 known properties: ..…........ All properties are mentioned here.....
At [source : (String) " response is given here"

【问题讨论】:

  • 呈现的代码中至少有两个语法错误。请确保这是产生错误的实际代码。此外,请正确格式化代码并遵循 getter 和 setter 的 java 命名约定 (gettrueAge() -> getTrueAge(), settrueAge(...) -> setTrueAge(...))。 Jackson 可能会依赖那些确切的 getter 和 setter。
  • 感谢您的快速回复。我已手动输入代码,因为我无法使用工作电脑。在实际应用中,我给出了正确的语法。

标签: java arrays string rest getter-setter


【解决方案1】:

您有两种方法可以忽略 POJO 中 Json 中的未知字段

通过使用注释

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    
@JsonIgnoreProperties(ignoreUnknown = true)
public class SumAll {

}

或通过预配置的 ObjectMapper 反序列化

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
    
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.readValue(json, SumAll.class);

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 2015-05-12
    • 1970-01-01
    • 2016-02-20
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多