如何将 Rest-Assure 反序列化为 TodoList?

时间:2021-01-05 09:57:35

标签: java json rest-assured

<块引用>

我想将 Rest-Assure 响应中的 Json 数组反序列化为 TodoList,并将 Todo 对象作为 List 中的元素。 我有一个 Todo 类

import java.util.Date;

public class Todo   {
    
    private Long id;
    
    private String name;
    
    private String description;
    
    private Date targetDate;
    
    private Boolean isDone;
    
    protected Todo() {
        
    }
    
    
    
    /**
        * Get the id of the bank.
        * @return id for the bank
        */
    public Long getId() {
            return id;
        }
    
    
    
    public Todo(String name, String description, Date targetDate, boolean isDone) {
        this.name = name;
        this.description = description;
        this.targetDate = targetDate;
        this.isDone = isDone;
    }



    public void setName( String name) {
         this.name = name;
    }
    
    public String getName() {
        return this.name;
    }



    public String getDescription() {
        return this.description;
    }



    public Date getTargetDate() {
        return this.targetDate;
    }



    public Boolean getIsDone() {
        return this.isDone;
    }



    public void setId(Long id) {
        this.id = id;
    }



    public void setDescription(String description) {
        this.description = description;
    }



    public void setTargetDate(Date targetDate) {
        this.targetDate = targetDate;
    }



    public void setIsDone(Boolean isDone) {
        this.isDone = isDone;
    }

}
<块引用>

这个类的对象在一个ListTodo类中管理

package com.steinko.todo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TodoList {
    
    private List<Todo> todos = new ArrayList<>();
    
    public void addTodo(Todo todo) {
        this.todos.add(todo);
    }
    
    public void addAllTodos(Collection<Todo> todos) {
        this.todos.addAll(todos);
    }
     
    public List<Todo> booksByAuthor(String author) {
        return todos.stream()
          .filter(book -> Objects.equals(author, book.getName()))
          .collect(Collectors.toList());
    }

}
<块引用>

我希望 Rest Assure 查询提供一个 TodoList。我想从 Json 数组转换为 TodoList 我该怎么做

TodoList result =  given().
                    when().
                      get("/user/stein/todos").??????

2 个答案:

答案 0 :(得分:1)

get 请求后,您可以提取 Response。然后,您可以获取 JsonPath 并在 ObjectMapper 的帮助下将任何 JSON 转换为 POJO

基于您的代码:

TodoList result =  given().
                    when().
                      get("/user/stein/todos")
                    then().
                    extract().
                    response(). //here's the Response object of Rest Assured
                    jsonPath(). //calling json path on response
                    getObject("$", TodoList.class);

线

getObject("$", TodoList.class);

告诉 JSON Path 将 JSON 转换为 TodoList.class 对象,从 JSON 的根开始。 如果你有这样的回应:

{
   "someKey": {
      "someOtherKey": {
          //here's todo list
       }

   }
}

那么您可以使用 someKey.someOtherKey 代替 $。就像告诉 Object Mapper 从哪里开始 JSON 转换

答案 1 :(得分:0)

试试这个:

TodoList result =  given()
                    .when()
                    .get("/user/stein/todos")
                    .then()
                    .extract()
                    .as(ToDoList.class);
相关问题