使用WebClient获取JSON对象的列表

发布于 2025-02-09 07:20:29 字数 1210 浏览 2 评论 0原文

我正在尝试使用WebClient提高异步Java技能。我有一个可获取和API的简单控制器,但它不会通过简单地进行“ bodytomono(sportresponse.class)”来映射

响应一种更清洁的方法来解决这个问题。当前的代码是可行的,它看起来很凌乱而不干净。

@Service
public class SportService {
    private final WebClient sportWebClient;
    private ApplicationConfiguration applicationConfiguration;

public SportService(WebClient webClient, ApplicationConfiguration applicationConfiguration) {
    this.sportWebClient = webClient;
    this.applicationConfiguration = applicationConfiguration;
}

public List<SportResponse> getAllSports() {
    return sportWebClient
            .get()
            .uri("api/v1/json/2/all_sports.php")
            .retrieve()
            .bodyToMono(LinkedHashMap.class)
            .map(this::mapResponse)
            .block();
}
private List<SportResponse> mapResponse(LinkedHashMap response) {
    ObjectMapper mapper = new ObjectMapper();
    List list = (ArrayList) response.get("sports");
    List<SportResponse> sportResponseList = (List<SportResponse>) list.stream()
            .map(item -> mapper.convertValue(item, SportResponse.class))
            .collect(Collectors.toList());
    return sportResponseList;
}

}

I'm trying to improve my async java skills using Webclient. I have this simple controller that fetch and api but It won't map the response by simply doing "bodyToMono(SportResponse.class)"

I've created this messy private method in order to map the Object and I'm wondering is there is a cleaner way to approach this. THE CURRENT CODE IS WORKS IT JUST LOOKS MESSY AND NOT CLEAN.

@Service
public class SportService {
    private final WebClient sportWebClient;
    private ApplicationConfiguration applicationConfiguration;

public SportService(WebClient webClient, ApplicationConfiguration applicationConfiguration) {
    this.sportWebClient = webClient;
    this.applicationConfiguration = applicationConfiguration;
}

public List<SportResponse> getAllSports() {
    return sportWebClient
            .get()
            .uri("api/v1/json/2/all_sports.php")
            .retrieve()
            .bodyToMono(LinkedHashMap.class)
            .map(this::mapResponse)
            .block();
}
private List<SportResponse> mapResponse(LinkedHashMap response) {
    ObjectMapper mapper = new ObjectMapper();
    List list = (ArrayList) response.get("sports");
    List<SportResponse> sportResponseList = (List<SportResponse>) list.stream()
            .map(item -> mapper.convertValue(item, SportResponse.class))
            .collect(Collectors.toList());
    return sportResponseList;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2025-02-16 07:20:29

您只需要使用嵌入式对象定义相应的POJO,杰克逊将自动进行映射

public List<SportResponse.Sport> getAllSports() {
    return sportWebClient
            .get()
            .uri("api/v1/json/2/all_sports.php")
            .retrieve()
            .bodyToMono(SportResponse.class)
            .map(SportResponse::getSports)
            .block();
}

@Data
private class SportResponse {
    @JsonProperty("sports")
    private List<Sport> sports;

    @Data
    private static class Sport {
        @JsonProperty("idSport")
        private String idSport;
        
       ...
    }
}

You just need to define corresponding POJO using embedded object and Jackson will do mapping automatically

public List<SportResponse.Sport> getAllSports() {
    return sportWebClient
            .get()
            .uri("api/v1/json/2/all_sports.php")
            .retrieve()
            .bodyToMono(SportResponse.class)
            .map(SportResponse::getSports)
            .block();
}

@Data
private class SportResponse {
    @JsonProperty("sports")
    private List<Sport> sports;

    @Data
    private static class Sport {
        @JsonProperty("idSport")
        private String idSport;
        
       ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文