springboot webflux mono 使用示例(中间操作)


springboot webflux mono 使用示例(中间操作)

 

 

*****************************

flatMap、map、transform:转换操作

 

class Person{

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        Person person = (Person) o;
        return Objects.equals(getName(), person.getName()) &&
                Objects.equals(getAge(), person.getAge());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test7 {

    public static void main(String[] args) {
        Mono.fromCallable(()->{
            Map<String,String> map=new HashMap<>();
            map.put("name","瓜田李下");
            map.put("age","20");

            return map;
        }).flatMap(map ->{
            Person person=new Person();
            person.setName(map.get("name"));
            person.setAge(Integer.parseInt(map.get("age")));

            return Mono.just(person);
        }).subscribe(System.out::println);

        Mono.fromCallable(()->{
            Map<String,String> map=new HashMap<>();
            map.put("name","瓜田李下");
            map.put("age","20");

            return map;
        }).map(map ->{
            Person person=new Person();
            person.setName(map.get("name"));
            person.setAge(Integer.parseInt(map.get("age")));

            return person;
        }).subscribe(System.out::println);

        Mono.fromCallable(()->{
            Map<String,String> map=new HashMap<>();
            map.put("name","瓜田李下");
            map.put("age","20");

            return map;
        }).transform(mono -> mono.map(map -> {
            Person person=new Person();
            person.setName(map.get("name"));
            person.setAge(Integer.parseInt(map.get("age")));

            return person;
        })).subscribe(System.out::println);

        Mono.fromCallable(()->{
            Map<String,String> map=new HashMap<>();
            map.put("name","瓜田李下");
            map.put("age","20");

            return map;
        }).transform(mono -> mono.flatMap(map -> {
            Person person=new Person();
            person.setName(map.get("name"));
            person.setAge(Integer.parseInt(map.get("age")));

            return Mono.just(person);
        })).subscribe(System.out::println);
    }
}

 

***********************

控制台输出

 

Person{name='瓜田李下', age=20}
Person{name='瓜田李下', age=20}
Person{name='瓜田李下', age=20}
Person{name='瓜田李下', age=20}

 

 

*****************************

filter :过滤操作

 

public class Test8 {

    public static void main(String[] args){
        Mono.just(2).filter(integer -> integer>1).subscribe(System.out::println);

        System.out.println("\n***************");
        Mono.just(2).filter(integer -> integer>2).subscribe(System.out::println);
    }
}

 

***********************

控制台输出

 

2

***************

操作2的内容被过滤掉,无输出

 

 

*****************************

do 操作

 

public class Test9 {

    public static void main(String[] args) throws Exception{
        Mono.just("hello world")
                .doOnNext(s->System.out.println("next"))
                .doFinally(s -> System.out.println("finally"))
                .doAfterTerminate(() -> System.out.println("terminate"))
                .subscribe(System.out::println);
    }
}

 

***********************

控制台输出

 

next
hello world
terminate
finally

 

 

*****************************

log 操作

 

public class Test10 {

    public static void main(String[] args) {
        Mono.just("hello world").subscribe(System.out::println);

        System.out.println("\n*************");
        Mono.just("hello world 2").log().subscribe(System.out::println);
    }
}

 

***********************

控制台输出

 

hello world

*************
17:56:07.063 [main] INFO reactor.Mono.Just.1 - | onSubscribe([Synchronous Fuseable] Operators.ScalarSubscription)
17:56:07.067 [main] INFO reactor.Mono.Just.1 - | request(unbounded)
17:56:07.068 [main] INFO reactor.Mono.Just.1 - | onNext(hello world 2)
hello world 2
17:56:07.070 [main] INFO reactor.Mono.Just.1 - | onComplete()

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值