在 REST Assured 中,将响应映射到对象时如何解决 UnrecognizedPropertyException?

Posted

技术标签:

【中文标题】在 REST Assured 中,将响应映射到对象时如何解决 UnrecognizedPropertyException?【英文标题】:In REST Assured, how do I solve UnrecognizedPropertyException when mapping response to an object? 【发布时间】:2018-12-05 13:29:58 【问题描述】:

我是 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;   

另一个:

public class RegistrationSuccessResponse   
    @JsonIgnoreProperties(ignoreUnknown = true)

    public String SuccessCode;   
    public String Message;

当我尝试通过 Testng.xml 文件运行上述代码时,出现以下错误:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“FaultId”(类 RegistrationFailureResponse),未标记为可忽略(0 个已知属性:]) 在 [来源:(字符串)“ "FaultId": "用户已经存在", “故障”:“FAULT_USER_ALREADY_EXISTS” "; line: 2, column: 17] (通过引用链:RegistrationFailureResponse["FaultId"]) 在 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60) 在 com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822) 在 com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152) 在 com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589) 在 com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567) 在 com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294) 在 com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) 在 com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) 在 com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042) 在 com.fasterxml.jackson.databind.ObjectMapper$readValue$0.call(未知来源) 在 org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) ......

请帮忙,我用谷歌搜索了解决方案,但没有一个与我在这里面临的问题有关

【问题讨论】:

Jackson with JSON: Unrecognized field, not marked as ignorable的可能重复 【参考方案1】:

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

 
  "FaultId": "User already exists",
  "fault": "FAULT_USER_ALREADY_EXISTS"

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

public class RegistrationFailureResponse 

    @JsonProperty private String FaultId;
    @JsonProperty private String fault;

【讨论】:

@AakashGoyal 我相信默认情况下 Jackson 会搜索 getter/setter 以查找类中的属性。因为您的班级没有这些,所以您需要注释。

以上是关于在 REST Assured 中,将响应映射到对象时如何解决 UnrecognizedPropertyException?的主要内容,如果未能解决你的问题,请参考以下文章

rest-assured的对象映射(Object Mapping)

rest-assured的对象映射(序列化和反序列化)

如何将JSON响应转换为Java List-使用Rest Assured进行API测试

rest-assured的日志使用介绍

rest-assured之获取响应数据(Getting Response Data)

是否可以使用 Java Stream api 重写带有 Rest-Assured 请求尝试的 WHILE 循环