Skip to content

Instantly share code, notes, and snippets.

View KalpaD's full-sized avatar

Kalpa Senanayake KalpaD

View GitHub Profile
{
"errors": [
{
"code": "firstName",
"message": "First name must be between 2 and 25 characters"
},
{
"code": "title",
"message": "Title cannot be null"
},
public class FormattedNameResponseMapper {
private FormattedNameResponseMapper() {
}
public static FormattedNameResponse fromWebExchangeBindException(WebExchangeBindException ex) {
FormattedNameResponse res = new FormattedNameResponse();
List<Error> errors = ex.getFieldErrors().stream()
.map(fieldError -> new Error(fieldError.getField(), fieldError.getDefaultMessage()))
.collect(Collectors.toList());
@RestController
public class ValidationDemoController {
@PostMapping("/format")
public Mono<ResponseEntity<FormattedNameResponse>> format(@Valid @RequestBody Mono<FormatNameRequest> request) {
return request
.map(res -> ResponseEntity.status(HttpStatus.OK).body(FormattedNameResponseMapper.fromFormatNameRequest(res)))
.onErrorResume(WebExchangeBindException.class,
ex -> Mono.just(ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(FormattedNameResponseMapper.fromWebExchangeBindException(ex))));
package org.kds.reactive.model;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class FormatNameRequest {
@NotNull(message = "Title cannot be null")
private String title;
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'java'
id 'idea'
}
apply plugin: 'io.spring.dependency-management'
group = 'org.kds.reactive'
version = '0.0.1-SNAPSHOT'
Request
POST http://host:port/format
Content-Type: application/json
{
"title":"Mr",
"firstName":"Jhon",
"middleName": "Martin",
"lastName": "Smith"
}
private static void debuggingUsingHook() {
Hooks.onOperatorDebug();
List<String> nameList = Arrays.asList("Rochel", "April", "Hong");
Flux<String> stringFlux = Flux.fromIterable(nameList)
.distinct()
.map(name -> name.substring(0, 3))
.map(String::toUpperCase)
.map(name -> {
private static void debuggingUsingCheckpoints() {
List<String> nameList = Arrays.asList("Rochel", "April", "Hong");
Flux<String> stringFlux = Flux.fromIterable(nameList)
.distinct()
.checkpoint("after distinct()")
.map(name -> name.substring(0, 3))
.checkpoint("after substring")
.map(String::toUpperCase)
.checkpoint("after toUpperCase")
private static void debuggingWithOutCheckpoints() {
List<String> nameList = Arrays.asList("Rochel", "April", "Hong");
Flux<String> stringFlux = Flux.fromIterable(nameList)
.distinct()
.map(name -> name.substring(0, 3))
.map(String::toUpperCase)
.map(name -> {
if (name.equals("HON")) {
throw new RuntimeException("Boom!");
private static void debuggingUsingLogOperator() {
List<String> nameList = Arrays.asList("Rochel", "April", "Hong");
Flux<String> stringFlux = Flux.fromIterable(nameList)
.distinct()
.map(name -> name.substring(0, 3))
.map(String::toUpperCase)
.log();
stringFlux.subscribe(log::info);