为了账号安全,请及时绑定邮箱和手机立即绑定

在REST安全中,将响应映射到对象时如何解决UnrecognizedProperty

在REST安全中,将响应映射到对象时如何解决UnrecognizedProperty

侃侃尔雅 2021-04-02 14:15:53
我是REST Assured API测试的新手,我已经在库中添加了jackson-databind,jackson-annotations和jackson-core所有2.9.6版本。下面是我的代码:public class SerializeAndDeserialize {    @Test    public void RegistrationSuccessful()    {           RestAssured.baseURI ="http://restapi.demoqa.com/customer";        RequestSpecification request = RestAssured.given();        JSONObject requestParams = new JSONObject();        requestParams.put("FirstName", "Virkjdender"); // Cast        requestParams.put("LastName", "Scklxjingh");        requestParams.put("UserName", "63userf2d3cn,mxd2011");        requestParams.put("Password", "passwors000mxzmd1");         requestParams.put("Email",  "ed26dfdasdf39@gmail.com");         request.body(requestParams.toJSONString());        Response response = request.post("/register");        ResponseBody body = response.getBody();        System.out.println(response.body().asString());        System.out.println(response.contentType());        System.out.println(response.getStatusCode());               if(response.statusCode() == 200)        {            // Deserialize the Response body into RegistrationFailureResponse            RegistrationFailureResponse responseBody = body.as(RegistrationFailureResponse.class);               Assert.assertEquals("User already exists", responseBody.FaultId);            Assert.assertEquals("FAULT_USER_ALREADY_EXISTS", responseBody.fault);           }        else if (response.statusCode() == 201)        {            // Deserialize the Response body into RegistrationSuccessResponse            RegistrationSuccessResponse responseBody = body.as(RegistrationSuccessResponse.class);                      Assert.assertEquals("OPERATION_SUCCESS", responseBody.SuccessCode);            Assert.assertEquals("Operation completed successfully", responseBody.Message);          }       }}和相关的类如下:public class RegistrationFailureResponse {      @JsonIgnoreProperties(ignoreUnknown = true)     String FaultId;    String fault;   }请帮忙,我用谷歌搜索了解决方案,但都与我在这里面临的问题无关
查看完整描述

1 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

实际上,您不应该忽略未知属性,因为没有属性。响应正是您所期望的:


  "FaultId": "User already exists",

  "fault": "FAULT_USER_ALREADY_EXISTS"

}

但是您的响应类配置错误。尝试使用@JsonProperty注释字段。


public class RegistrationFailureResponse {


    @JsonProperty private String FaultId;

    @JsonProperty private String fault;

}


查看完整回答
反对 回复 2021-04-28
  • 1 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信