Spring框架WebFlux反应式编程
我正在尝试向端点发送对象,但我不明白为什么不能使用.get(),为什么必须使用.post()?如果endpoint方法获取一个对象并对其执行某些操作并返回一个对象,该怎么办?我可能希望向端点发送一个对象,该端点将该对象作为参数。有办法吗?如何将customer对象传递给getCustomer()端点Spring框架WebFlux反应式编程,spring,spring-boot,spring-mvc,webclient,spring-webclient,Spring,Spring Boot,Spring Mvc,Webclient,Spring Webclient,我正在尝试向端点发送对象,但我不明白为什么不能使用.get(),为什么必须使用.post()?如果endpoint方法获取一个对象并对其执行某些操作并返回一个对象,该怎么办?我可能希望向端点发送一个对象,该端点将该对象作为参数。有办法吗?如何将customer对象传递给getCustomer()端点 WebClient.create("http://localhost:8080") .get()//why this can not be used? why post ha
WebClient.create("http://localhost:8080")
.get()//why this can not be used? why post has to be used?
.uri("client/getCustomer")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(customer)//with .get() body cannot be passed.
.retrieve()
.bodyToMono(Customer.class);
@GET
@Path("/getCustomer")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Customer getCustomer(Customer customer) {
//do something
return customer;
}
已编辑 在GET方法中,数据在URL中发送。就像: 在POST方法中,数据存储在 HTTP请求 因此,我们不应该期望.get()方法具有.bodyValue()。 现在,如果您想使用GET方法发送数据,您应该在URL中发送它们,如下面的代码段所示
WebClient.create("http://localhost:8080")
.get()
.uri("client/getCustomer/{customerName}" , "testName")
.retrieve()
.bodyToMono(Customer.class);
有用的Spring webClient示例:
有关POST和GET的更多信息
我的意思是为什么不能同时使用.get()和.bodyValue(客户)?为什么必须使用.post()才能将对象作为requestbody发送到端点?对此没有任何规则。您可以使用任何http方法发送(或省略)正文。@Ritesh您可以使用这两种方法发送数据,但方式不同。GET请求不能有消息正文。但是您仍然可以使用URL参数向服务器发送数据。没有这样的规则说“GET请求不能有消息体”。如果你愿意,你可以寄过去。看见