SpringWebFlux提供模拟CRUD的RouteFunction类型的api请求

本文介绍了如何在SpringBootWebFlux框架中使用CRUD操作构建RouterFunction,包括GET、POST请求处理以及数据验证。通过`PersonHandler`和`PersonRepository`组件展示了RESTfulAPI的创建和响应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package luck.spring.boot.webflux;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.function.BiFunction;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.notFound;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@Configuration
public class CurdRouterFunctionConfig {

    // 定义使用CRUD的handler进行逻辑处理的嵌套路由
    @Bean
    public RouterFunction<ServerResponse> nestPersonRouterFunction(PersonHandler handler) {
        // 定义嵌套路由
        return route()
                .path("/person", b1 -> b1
                        .nest(accept(APPLICATION_JSON), b2 -> b2
                                // 使用CRUD的handler进行逻辑处理
                                .GET("/{id}", handler::getPerson)
                                .GET("", handler::listPeople))
                        .POST("/person", handler::createPerson))
                .build();
    }

    @Component
    static class PersonRepository {

        public Flux<Person> allPeople() {
            return Flux.just(new Person(1, "luck"), new Person(2, "java"));
        }

        public Mono<Person> getPerson(int personId) {
            return Mono.just(new Person(1, "luck"));
        }

        public BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>> savePerson(Mono<Person> person) {
            return (exchange, context) -> {
                ServerHttpResponse response = exchange.getResponse();
                DataBuffer buffer = response.bufferFactory().wrap("保存成功".getBytes(StandardCharsets.UTF_8));
                return response.writeWith(Mono.just(buffer));
            };
        }
    }


    @Component
    static class PersonHandler {
        private final Validator validator;
        private final PersonRepository repository;

        public PersonHandler(PersonRepository repository) {
            this.repository = repository;
            this.validator = new LocalValidatorFactoryBean();
        }

        public Mono<ServerResponse> listPeople(ServerRequest request) {
            Flux<Person> people = repository.allPeople();
            return ok().contentType(APPLICATION_JSON).body(people, Person.class);
        }

        public Mono<ServerResponse> createPerson(ServerRequest request) {
            Mono<Person> person = request.bodyToMono(Person.class).doOnNext(this::validate);
            return ok().build(repository.savePerson(person));
        }

        public Mono<ServerResponse> getPerson(ServerRequest request) {
            int personId = Integer.parseInt(request.pathVariable("id"));
            return repository.getPerson(personId)
                    .flatMap(person -> ok().contentType(APPLICATION_JSON).bodyValue(person))
                    .switchIfEmpty(notFound().build());
        }

        private void validate(Person person) {
            Errors errors = new BeanPropertyBindingResult(person, "person");
            validator.validate(person, errors);
            if (errors.hasErrors()) {
                throw new ServerWebInputException(errors.toString());
            }
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值