You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular与Spring Boot中链式发送HTTP请求示例求助

两次接口调用实现示例

1. 定义对应实体类

首先需要创建与接口返回JSON结构匹配的实体类:

Demand类

import java.util.List;

public class Demand {
    private Long id;
    // 其他Demand相关业务字段
    private List<Contract> contracts;

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

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

    public List<Contract> getContracts() {
        return contracts;
    }

    public void setContracts(List<Contract> contracts) {
        this.contracts = contracts;
    }
}

Contract类

public class Contract {
    private Long id;
    // 其他Contract相关业务字段

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

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

2. 使用RestTemplate实现调用(Spring环境)

import org.springframework.web.client.RestTemplate;

public class ApiCallExample {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        
        // 第一步:获取Demand对象,提取Contract ID
        String demandUrl = "http://localhost:8080/v1/demande/getDemande/1";
        Demand demand = restTemplate.getForObject(demandUrl, Demand.class);
        
        if (demand != null && demand.getContracts() != null && !demand.getContracts().isEmpty()) {
            // 此处取列表第一个Contract的ID作为示例,可根据实际业务逻辑调整获取规则
            Long contractId = demand.getContracts().get(0).getId();
            
            // 第二步:根据Contract ID获取对应Contract对象
            String contractUrl = String.format("http://localhost:8080/v1/demande/getContrat/%d", contractId);
            Contract contract = restTemplate.getForObject(contractUrl, Contract.class);
            
            // 后续业务处理
            System.out.println("获取到的Contract ID: " + contract.getId());
        }
    }
}

3. 使用OkHttp实现调用(非Spring环境)

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.List;

public class OkHttpApiCallExample {
    private static final OkHttpClient client = new OkHttpClient();
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {
        // 第一步:请求获取Demand对象
        Request demandRequest = new Request.Builder()
                .url("http://localhost:8080/v1/demande/getDemande/1")
                .build();
        
        try (Response demandResponse = client.newCall(demandRequest).execute()) {
            if (!demandResponse.isSuccessful()) throw new IOException("请求失败: " + demandResponse);
            
            Demand demand = objectMapper.readValue(demandResponse.body().string(), Demand.class);
            
            if (demand != null && demand.getContracts() != null && !demand.getContracts().isEmpty()) {
                Long contractId = demand.getContracts().get(0).getId();
                
                // 第二步:请求获取对应Contract对象
                Request contractRequest = new Request.Builder()
                        .url(String.format("http://localhost:8080/v1/demande/getContrat/%d", contractId))
                        .build();
                
                try (Response contractResponse = client.newCall(contractRequest).execute()) {
                    if (!contractResponse.isSuccessful()) throw new IOException("请求失败: " + contractResponse);
                    
                    Contract contract = objectMapper.readValue(contractResponse.body().string(), Contract.class);
                    System.out.println("获取到的Contract ID: " + contract.getId());
                }
            }
        }
    }
}

注意:使用OkHttp需引入对应依赖,Maven依赖配置如下:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.11.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>

内容的提问来源于stack exchange,提问作者BAKHALED Ibrahim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:12:41