Posts

Showing posts with the label spring-webflux

How to produce metrics of URI correctly in spring boot 2 + webflux

How to produce metrics of URI correctly in spring boot 2 + webflux I have a problem using spring boot 2.0.3 + spring webflux (functional endpoints) + metrics. According to the official doc: The generated metrics of uri should be the request’s URI template prior to variable substitution Which means instead of /api/person/123, the generated metrics of URI should be something like /api/person/{id}. However, when I query those metrics on my spring boot app with this link http://localhost:8080/actuator/metrics/http.server.requests, I got these URIs: /api/person/2 /api/person/33 /api/person/12 To reproduce this issue, I put a sample code here: https://gitlab.com/itsleon/demo Can someone please shed some light on this issue? Thanks in advance! 1 Answer 1 There's an open issue for that in Spring Boot. Currently, Spring WebFlux fn doesn't provide the information about the URI pattern that matched fo...

How to catch exceptions from spring webflux controller?

How to catch exceptions from spring webflux controller? I am using javax validations in my controller with @Valid @RequestBody . When the server receives invalid data it throws error but I want to handle that error and return custom formatted error. I am unable to catch exception in my controller advice. I am using spring webFlux so can't use the bindingResult. How can I handle that exception? Here is my code @Valid @RequestBody Controller @PostMapping fun createPerson(@Valid @RequestBody resource: PersonResource): Mono<Person> { return personService.save(resource.toPerson()) } Resource data class PersonResource( val id: String?, @field:NotEmpty val name: String, ... } ErrorHandler @ControllerAdvice class ApiErrorHandler { @ExceptionHandler(IllegalArgumentException::class) fun handleValidationErrors(e: IllegalArgumentException): ResponseEntity<*> { // never reaches here } } Maybe this stackoverf...