首页 > 解决方案 > WebClient 将数据保存到 Mongo

问题描述

我使用 WebClient 从外部 api 检索了一些关于客户端的数据。现在我想将它保存到 MongoDb,但正在努力解决它。这里我是如何检索数据的:

public class Main {
    public static void main(String[] args) throws URISyntaxException, MalformedURLException {
        WebClient client = WebClient.create();
        RequestDTO requestBody = new RequestDTO("getClients", new RequestDataDto("1", "60"));

        ProductWrapperMars response = client.post()
                .uri(new URI("MyURL"))
                .header("token", "MyToken") //I have a header as well
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .bodyValue(requestBody)
                .retrieve()
                .bodyToMono(ClientList.class)
                .block();       
    }
}

下面是 Client 类的样子:

public class ClientList {

    
    private Boolean available;
    private List<Clients> data;

}


public class Clients {

    
    @Id
    private Long id;
    private String name;
    private Timestamp date;

//getters and setters
}

我应该如何组织将检索到的数据保存到 Mongo?

标签: javamongodbspring-bootspring-webclient

解决方案


推荐阅读