Tag: 项目反应堆

如何在Spring WebFlux中记录请求和响应主体

我想用Kotlin在Spring WebFlux的REST API中集中记录请求和响应。 到目前为止我已经尝试过这种方法 @Bean fun apiRouter() = router { (accept(MediaType.APPLICATION_JSON) and “/api”).nest { “/user”.nest { GET(“/”, userHandler::listUsers) POST(“/{userId}”, userHandler::updateUser) } } }.filter { request, next -> logger.info { “Processing request $request with body ${request.bodyToMono()}” } next.handle(request).doOnSuccess { logger.info { “Handling with response $it” } } } 这里的请求方法和路径日志成功,但身体是Mono ,所以我应该如何登录? 应该是相反的方式,我必须订阅请求正文Mono ,并在回调登录? 另一个问题是ServerResponse接口在这里不能访问响应主体。 我怎样才能在这里? 我试过的另一种方法是使用WebFilter @Bean […]

Spring WebFlux:Reactive MongoDB

我是Spring Reactor的新手,所以我想重构这个简单的spring数据(在kotlin)方法: fun save(user: User): Mono { if (findByEmail(user.email).block() != null) { throw UserAlreadyExistsException() } user.password = passwordEncoder.encode(user.password) return userRepository.save(user) } 谢谢

Spring WebFlux:只有一个连接接收用户允许

我正在用Spring 5 Webflux和Kotlin编写一个简单的应用程序。 我正在尝试以下列方式实现PUT端点: PUT(“/confs/{id}”, { val id = it.pathVariable(“id”) ServerResponse.ok().body(service.save(it.bodyToMono(Item::class.java)), Item::class.java) }) 保存技巧是我尝试从项目中读取城市名称,解析地理坐标,在原始项目中覆盖它们,然后使用Spring Data Mongo Reactive回购保存到Mongo。 fun save(item: Mono): Mono { val geo = item.flatMap { val city = it.location?.city ?: “Somewhere” geoService.resolveGeoFromCity(city) } val zipped = item.zipWith(geo) .map { it.t1.location?.geo = it.t2 it.t1 } return repo.saveAll(zipped) .toMono() } 解析地理坐标的代码在这里: @Service class GeoService() { […]

如何用Spring WebFlux返回404

我有一个这样的控制器(在Kotlin中): @RestController @RequestMapping(“/”) class CustomerController (private val service: CustomerService) { @GetMapping(“/{id}”) fun findById(@PathVariable id: String, @RequestHeader(value = IF_NONE_MATCH) versionHeader: String?): Mono<HttpEntity> = return service.findById(id) .switchIfEmpty(Mono.error(NotFoundException())) .map { // ETag stuff … ok().eTag(“…”).body(…) } } 我想知道是否有一个更好的方法比抛出一个exception是用@ResponseStatus(code = NOT_FOUND)注解@ResponseStatus(code = NOT_FOUND)

不明白如何使通量订阅在kotlin工作

我是新来的反应式编程。 我期待看到 test provider started Beat 1000 Beat 2000 在日志中,但是只有test provider started并且没有Beat或on complete消息。 看起来我想念一些东西 @Service class ProviderService { @PostConstruct fun start(){ val hb: Flux = Flux.interval(Duration.ofSeconds(1)).map { HeartBeat(it) } val provider = Provider(“test”, hb) } } //////////////////////// open class Provider(name: String, heartBests: Flux) { companion object { val log = LoggerFactory.getLogger(Provider::class.java)!! } init { log.info(“$name […]