1、web的配置
原来mvc是引入WebMvcConfigurer ,需改成WebFluxConfigurer
注解EnableWebMvc改成EnableWebFlux
2、全局异常的配置
webflux有提供异常处理类,但也保留支持注解:ControllerAdvice,RestControllerAdvice等。
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} to indicate when exception handling is complete
*/
@Override
public Mono 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 handle(Throwable ex) {
return createResponse(INTERNAL_SERVER_ERROR, ex);
}
private Mono 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> 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 viewResolvers() {
return this.handlerStrategies.viewResolvers();
}
}
}
3、统一返回配置
@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");
}
}
只是换个注解
4、swagger配置
很可惜的说一句,swagger不支持webflux,官方不支持,没办法。
后来在寻求解决办法时,我在github发现在有人定制了springfox-spring-webflux,但这个jar我下载不了,各位有兴趣可以看看
5、shiro配置
最可惜的是shiro不支持webflux,我现在项目转成webflux,无法使用。
有人建议我转成security,但考虑成本,只能放弃!
6、interceptor实现,拦截HandlerMethod
webflux已经没有了Interceptor的概念,但是可以通过WebFilter的方式实现
@Component
public class CustomWebFilter implements WebFilter {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
public Mono filter(ServerWebExchange exchange, WebFilterChain chain) {
Object handlerMethod = requestMappingHandlerMapping.getHandler(exchange).toProcessor().peek();
//注意跨域时的配置,跨域时浏览器会先发送一个option请求,这时候getHandler不会时真正的HandlerMethod
if(handlerMethod instanceof HandlerMethod){
Valid valid = ((HandlerMethod) handlerMethod).getMethodAnnotation(Valid.class);
//do your logic
}
//preprocess()
Mono response = chain.filter(exchange);
//postprocess()
return response;
}
}
转载链接:https://www.jianshu.com/p/96039cfddbda
总之这几天在搞API网关,一开始想用springcloud gateway,但坑太多了,要从spring mvc转到spring webflux就走了很多的弯路,成本有点大,最后下面链接探讨有关springcloud--网关的选取,仁者见仁,智者见智吧
https://www.jianshu.com/p/0acf308cf59a