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

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

}


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


侃侃尔雅
浏览 196回答 1
1回答

HUX布斯

实际上,您不应该忽略未知属性,因为没有属性。响应正是您所期望的:{   "FaultId": "User already exists",  "fault": "FAULT_USER_ALREADY_EXISTS"}但是您的响应类配置错误。尝试使用@JsonProperty注释字段。public class RegistrationFailureResponse {    @JsonProperty private String FaultId;    @JsonProperty private String fault;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java