public interface UserClient {
@GetExchange("/users")
Flux<User> getAll();
}
Spring会在运行时提供接口和exchange实现,我们只需要调用getAll()方法。
@Autowired
UserClient userClient;
userClient.getAll().subscribe(
data -> log.info("User: {}", data)
);
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- For reactive support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
@PutExchange
void update(@PathVariable Long id, @RequestBody User user);
//阻塞性
@GetExchange("/{id}")
User getById(...);
//Reactive
@GetExchange("/{id}")
Mono<User> getById(...);
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leftso.app.web.UserClient;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@Configuration
public class WebConfig {
@Bean
WebClient webClient(ObjectMapper objectMapper) {
return WebClient.builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.build();
}
@SneakyThrows
@Bean
UserClient postClient(WebClient webClient) {
HttpServiceProxyFactory httpServiceProxyFactory =
HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
.build();
return httpServiceProxyFactory.createClient(UserClient.class);
}
}
import com.leftso.app.model.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.DeleteExchange;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PostExchange;
import org.springframework.web.service.annotation.PutExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@HttpExchange(url = "/users", accept = "application/json", contentType = "application/json")
public interface UserClient {
@GetExchange("/")
Flux<User> getAll();
@GetExchange("/{id}")
Mono<User> getById(@PathVariable("id") Long id);
@PostExchange("/")
Mono<ResponseEntity<Void>> save(@RequestBody User user);
@PutExchange("/{id}")
Mono<ResponseEntity<Void>> update(@PathVariable Long id, @RequestBody User user);
@DeleteExchange("/{id}")
Mono<ResponseEntity<Void>> delete(@PathVariable Long id);
}
注意,我们创建了一个User类型的记录来保存用户信息。
public record User(Long id, String name, String username, String email) {}
现在我们可以将UserClient bean注入到应用程序类中,并调用方法以获取API响应。
@Autowired
UserClient userClient;
//获取所有用户
userClient.getAll().subscribe(
data -> log.info("User: {}", data)
);
//通过id获取用户
userClient.getById(1L).subscribe(
data -> log.info("User: {}", data)
);
//创建一个新用户
userClient.save(new User(null, "Lokesh", "lokesh", "admin@email.com"))
.subscribe(
data -> log.info("User: {}", data)
);
//通过id删除用户
userClient.delete(1L).subscribe(
data -> log.info("User: {}", data)
);
地址:https://www.leftso.com/article/1117.html