spring webmvc转webflux的初步尝试

2018-12-13  本文已影响0人  爱余星痕

最近在看api网关的源码,发现他用的是webflux,对这个挺感兴趣,所以尝试将手上的项目改成webflux

Component
public class ExceptionHandlerAdvice  implements WebExceptionHandler {

    /**
     * Handle the given exception. A completion signal through the return value
     * indicates error handling is complete while an error signal indicates the
     * exception is still not handled.
     *
     * @param exchange the current exchange
     * @param ex       the exception to handle
     * @return {@code Mono<Void>} to indicate when exception handling is complete
     */
    @Override
    public Mono<Void> handle(final ServerWebExchange exchange, final Throwable ex) {
        return handle(ex).flatMap(it -> it.writeTo(exchange,
                new HandlerStrategiesResponseContext(HandlerStrategies.withDefaults())))
                .flatMap(i -> Mono.empty());
    }


    private Mono<ServerResponse> handle(Throwable ex) {
        return createResponse(INTERNAL_SERVER_ERROR,   ex);
    }

    private Mono<ServerResponse> createResponse(final HttpStatus httpStatus, Throwable ex) {
        ErrorResponseData data = new ErrorResponseData();
        data.setCode(HttpCode.INTERNAL_SERVER_ERROR);
        data.setMsg(ex.getMessage());
        data.setStackMsg(ExceptionUtils.getStackTrace(ex));
        return ServerResponse.status(httpStatus).syncBody(data);
    }

    /**
     * The type Handler strategies response context.
     */
    static class HandlerStrategiesResponseContext implements ServerResponse.Context {

        private HandlerStrategies handlerStrategies;

        /**
         * Instantiates a new Handler strategies response context.
         *
         * @param handlerStrategies the handler strategies
         */
        public HandlerStrategiesResponseContext(final HandlerStrategies handlerStrategies) {
            this.handlerStrategies = handlerStrategies;
        }

        /**
         * Return the {@link HttpMessageWriter}s to be used for response body conversion.
         *
         * @return the list of message writers
         */
        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return this.handlerStrategies.messageWriters();
        }

        /**
         * Return the  {@link ViewResolver}s to be used for view name resolution.
         *
         * @return the list of view resolvers
         */
        @Override
        public List<ViewResolver> viewResolvers() {
            return this.handlerStrategies.viewResolvers();
        }
    }


}

@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

    @Bean
    public HeaderHttpSessionIdResolver httpSessionStrategy() {
        return new HeaderHttpSessionIdResolver("x-auth-token");
    }
}

需要改为

@Configuration
@EnableRedisWebSession
public class RedisSessionConfig {
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

    @Bean
    public HeaderHttpSessionIdResolver httpSessionStrategy() {
        return new HeaderHttpSessionIdResolver("x-auth-token");
    }
}

只是换个注解

虽说这次没有转成功,但对webflux还是有一定的了解,我认为webflux后面会火起来的,因为它的性能大大的PK原来的webmvc

上一篇下一篇

猜你喜欢

热点阅读