Spring Boot实体类中创建嵌套JSON对象并推送至指定API方法
解决方案
一、创建带Getter/Setter的实体类
1. 先修正JSON格式问题
你提供的JSON里数组符号[]使用错误,每个API对应的是单个对象,应该用{},正确格式如下:
{ "firstApi": { "id": 1, "name": "Ron" }, "secondApi": { "id": 2, "name": "Gita" }, "thirdApi": { "id": 3, "name": "Mita" } }
2. 引入Lombok简化代码(可选但推荐)
Spring Boot里可以用Lombok自动生成Getter/Setter,不用手动编写,先在依赖管理文件中添加依赖:
- Maven(
pom.xml):
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
- Gradle(
build.gradle):
compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'
3. 创建基础实体类(User)
对应每个API返回的id和name字段:
import lombok.Getter; import lombok.Setter; @Getter @Setter public class User { private Integer id; private String name; }
如果不想用Lombok,手动编写Getter/Setter版本:
public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4. 创建顶层实体类(ApiPayload)
对应整个JSON的结构,包含三个API对应的User对象:
import lombok.Getter; import lombok.Setter; @Getter @Setter public class ApiPayload { private User firstApi; private User secondApi; private User thirdApi; }
同样,手动编写Getter/Setter的版本只需给每个属性添加对应的get/set方法即可。
二、推送数据到指定API
Spring Boot常用两种HTTP客户端实现推送,以下分别给出示例:
1. 使用RestTemplate(同步方式)
第一步:注册RestTemplate Bean
在配置类或启动类中添加如下代码,让Spring容器管理RestTemplate:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
第二步:编写推送逻辑
注入RestTemplate,分别向三个API发送POST请求:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class ApiPushService { private final RestTemplate restTemplate; @Autowired public ApiPushService(RestTemplate restTemplate) { this.restTemplate = restTemplate; } public void pushData(ApiPayload payload) { // 推送至firstApi String firstApiUrl = "https://your-first-api-domain.com/endpoint"; restTemplate.postForObject(firstApiUrl, payload.getFirstApi(), String.class); // 推送至secondApi String secondApiUrl = "https://your-second-api-domain.com/endpoint"; restTemplate.postForObject(secondApiUrl, payload.getSecondApi(), String.class); // 推送至thirdApi String thirdApiUrl = "https://your-third-api-domain.com/endpoint"; restTemplate.postForObject(thirdApiUrl, payload.getThirdApi(), String.class); } }
2. 使用WebClient(响应式方式,适配Spring WebFlux)
第一步:注册WebClient Bean
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.client.WebClient; @Configuration public class WebClientConfig { @Bean public WebClient webClient() { return WebClient.create(); } }
第二步:编写推送逻辑
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @Service public class ApiPushService { private final WebClient webClient; @Autowired public ApiPushService(WebClient webClient) { this.webClient = webClient; } public void pushData(ApiPayload payload) { // 推送至firstApi Mono<String> firstResponse = webClient.post() .uri("https://your-first-api-domain.com/endpoint") .bodyValue(payload.getFirstApi()) .retrieve() .bodyToMono(String.class); firstResponse.block(); // 阻塞等待响应,非阻塞场景可移除block() // 推送至secondApi Mono<String> secondResponse = webClient.post() .uri("https://your-second-api-domain.com/endpoint") .bodyValue(payload.getSecondApi()) .retrieve() .bodyToMono(String.class); secondResponse.block(); // 推送至thirdApi Mono<String> thirdResponse = webClient.post() .uri("https://your-third-api-domain.com/endpoint") .bodyValue(payload.getThirdApi()) .retrieve() .bodyToMono(String.class); thirdResponse.block(); } }
3. 调用推送服务
在Controller中注入推送服务,接收请求后触发推送:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class PushController { private final ApiPushService pushService; @Autowired public PushController(ApiPushService pushService) { this.pushService = pushService; } @PostMapping("/push-data") public String pushData(@RequestBody ApiPayload payload) { pushService.pushData(payload); return "数据推送完成"; } }
内容的提问来源于stack exchange,提问作者Atique Ahmed
相关产品推荐
相关产品推荐

