前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring webmvc转webflux的初步尝试

spring webmvc转webflux的初步尝试

作者头像
星痕
发布2018-12-24 10:38:43
2.6K1
发布2018-12-24 10:38:43
举报
文章被收录于专栏:JAVA后端开发JAVA后端开发

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

  • web的配置 原来mvc是引入WebMvcConfigurer ,需改成WebFluxConfigurer 注解EnableWebMvc改成EnableWebFlux
  • 全局异常的配置 原来我是使用ControllerAdvice进行统一异常管理,但webflux有提供异常处理类。 示例代码如下:
代码语言:javascript
复制
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();
        }
    }


}
  • 统一返回配置 webflux做不到统一返回的配置,因为他不知道你需要返回的是Flux还是Mono
  • 共享会话配置 原来的公享会话配置为
代码语言:javascript
复制
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

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

需要改为

代码语言:javascript
复制
@Configuration
@EnableRedisWebSession
public class RedisSessionConfig {
    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

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

只是换个注解

  • swagger配置 很可惜的说一句,swagger不支持webflux,官方不支持,没办法。 后来在寻求解决办法时,我在github发现在有人定制了springfox-spring-webflux,但这个jar我下载不了,各位有兴趣可以看看
  • shiro配置 最可惜的是shiro不支持webflux,我现在项目转成webflux,无法使用。 有人建议我转成security,但考虑成本,只能放弃!

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档