Tag: 弹簧webflux

如何在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:只有一个连接接收用户允许

我正在用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)