如何使用webclient将数据从一个API发送到另一个API



我有两个springboot微服务api_A在localhost:8080上运行,api_B在localhost8081上运行。如何使用Web客户端将数据传输对象从api_A发送到api_B?

我有一个Person类,我想把这个Object作为json发送到端点localhost:8081/new person上的api_B。

@Data
@Builder
public class Person {
String name;
int age;
}

目前,我在api_A中有一个类,用于在api_B启动时向其发送数据。

public class Config {
@Autowired 
private WebClient.Builder webClientBuilder;
@PostConstruct
public Mono<Person> send(){
WebClient webClient = webClientBuilder.baseUrl("http://localhost:8080").build();
Person person = Person.builder().build();
return webClient.post()
.uri("/new-person")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just(person), Person.class)
.retrieve()
.bodyToMono(Person.class);
}
}

我认为这不起作用,因为每当我转到localhost:8081/new person时,我都会收到一个错误。非常感谢。

WebClient webClient = WebClient.create("http://localhost:8080");
public Mono<Something> getSomething(int id) {
webClient.get()
.uri("uri", parameters)
.retrieve()
.bodyToMono(Something.class);
}

最新更新