IllegalStateException: Could not resolve view with name
最近遇到一个重定向的问题困扰了很久,百度也找不到合适的解决方案!!
一 、问题描述
使用的技术:springboot(2.6.x) + springcloud gateway(3.1.x);
1、请求后台controller,通过service层进行一些列业务处理后,最终重定向到指定的页面显示数据;这里贴一下测试代码的demo
// 允许跨域访问@CrossOrigin(origins = "*", maxAge = 7200)@RequestMapping(value = "/test/request", method = RequestMethod.POST)public String demo1(@RequestBody Map<String, Object> params)throws IOException {System.out.println("接收到的报文信息:" + params );//TODO 业务处理代码略......String targetUrl = "redirect:http://www.baidu.com";return targetUrl;}
2、但重定向失败,提示了下面的报错问题
ERROR 17580 --- [nio-8080-exec-2] a.w.r.e.AbstractErrorWebExceptionHandler : [54db0557] 500 Server Error for HTTP POST "/request/test/request"java.lang.IllegalStateException: Could not resolve view with name 'redirect:http://www.baidu.com'.at org.springframework.web.reactive.result.view.ViewResolutionResultHandler.lambda$resolveViews$3(ViewResolutionResultHandler.java:278) ~[spring-webflux-5.3.15.jar:5.3.15]Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):*__checkpoint ⇢ Handler com.example.spring263.ApiRedirectController#demo1(Map) [DispatcherHandler]*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]*__checkpoint ⇢ HTTP POST "/request/test/request" [ExceptionHandlingWebHandler]
二、分析原因
1、根据上述的报错,断点ViewResolutionResultHandler的278行,对应的方法;如下
private Mono<List<View>> resolveViews(String viewName, Locale locale) {return Flux.fromIterable(getViewResolvers()).concatMap(resolver -> resolver.resolveViewName(viewName, locale)).collectList().map(views -> {if (views.isEmpty()) {throw new IllegalStateException("Could not resolve view with name '" + viewName + "'.");}views.addAll(getDefaultViews());return views;});}
发现是因为getViewResolvers()
没有找到视图解析器导致,无法解析redirect
关键字导致无法跳转到指定的地址上;
那么到底为什么没有get
到视图解析器呢?启动springboot工程的时候没加载到容器??
三、解决
网上搜罗了一通,什么baidu啊,bing啊,stackoverflow啊,都没找到相对满意的答案,心累…
还是看看官方文档吧,好像看英文又有点头大,还是硬着头皮啃一下吧,毕竟有翻译工具;
传送门:https://docs.spring.io/spring-framework/docs/5.3.15/reference/html/web-reactive.html#spring-webflux
好了发现一个相关的问题:如图
redirect
关键字需要**UrlBasedViewResolver**
及其子类才能解析;
上面导致无法重定向的原因是因为无法找到视图解析器(viewResolver)而报错Could not resolve view with name 'redirect:http://www.baidu.com'
;那我在启动的时候加一个是不是就可以了呢??那怎么加啊???
接着往下看吧。
首先我们的定义的重定向的controller类不能基于@RestController 注解,必需是@Controller
注解
接着就添加一个config去初始化我们的viewResolver
相关配置,自定义一个config类然后实现WebFluxConfigurer
接口,再重写接口里的方法就可以做相应的操作了。
加载一个视图解析器,那就找找这个WebFluxConfigurer
有没有对应的方法。
WebFluxConfigurer
的方法如下:
/*** Configure view resolution for rendering responses with a view and a model,* where the view is typically an HTML template but could also be based on* an HTTP message writer (e.g. JSON, XML).* <p>The configured view resolvers will be used for both annotated* controllers and functional endpoints.*/
default void configureViewResolvers(ViewResolverRegistry registry) {}
最终写的配置类如下:
@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {@Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {System.out.println("加载了WebFlux");ViewResolver viewResolver = new UrlBasedViewResolver();registry.viewResolver(viewResolver);}}
温馨提示:配置类必需的注解**@Configuration
**及**@EnableWebFlux**
因为上面官网说了,需要UrlBasedViewResolver
识别redirect
指令,所以就用了UrlBasedViewResolver
;
重启应用验证一下这个config配置类是否解决重定向问题!!!!!!
果然已经不报错,且跳转到百度首页了。再断点看看刚刚ViewResolutionResultHandler的resolveViews方法,是否有我们添加的UrlBasedViewResolver;
显然是它。
这个方法可以解决重定向的问题,但会不会引起其他问题,需要去到每个人的代码实践里,我自己分析我们的项目,目前是没什么影响的。
至于更深层的原因,欢迎大神评论区告诉我。。。。。
IllegalStateException: Could not resolve view with name相关推荐
- javax.servlet.ServletException: Could not resolve view with name ‘***‘ in servlet
javax.servlet.ServletException: Could not resolve view with name 'employee/login' in servlet with na ...
- javax.servlet.ServletException: Could not resolve view with name 'destination/isOtherExist' in servl
五月 28, 2016 5:36:38 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() fo ...
- 解决控制台Could not resolve view with name ...
关于 Could not resolve view with name 'xxx' in servlet with name 'spring-mvc' 的解决方法 1.原因之一,没有写"@R ...
- Could not resolve view with name '/xx.html' in servlet with name 'dispatcherServlet'
springboot 项目启动后访问网页,控制台报错: Could not resolve view with name '/xx.html' in servlet with name 'dispat ...
- Spring Boot问题之JSP无法显示Could not resolve view with name ‘xxxx‘ in servlet with name ‘dispatcherServlet
问题: 在Spring Boot应用中JSP页面无法显示, 页面提示: 控制台的错误信息类似: javax.servlet.ServletException: Could not resolve vi ...
- Could not resolve view with name 'xxx' in servlet with name 'dispatcherServlet',
1,问题 本人启动spring boot项目后,访问接口,报错信息如下: {"timestamp": 1537234637805,"status": 500,& ...
- 造成javax.servlet.ServletException: Could not resolve view with name的各种原因及解决方法
报错完成信息: javax.servlet.ServletException: Could not resolve view with name '/lose/index' in servlet wi ...
- Could not resolve view with name ‘updatelog/saveDetail’
Could not resolve view with name 'updatelog/saveDetail' in servlet with name 'dispatcherServlet'异常 数 ...
- redirect失败的原因,Could not resolve view with name
创建一个新的视图解释器 @Order 默认就是优先级最低 @Bean@Orderpublic InternalResourceViewResolver internalResourceViewReso ...
最新文章
- RHCE课程-RH253Linux服务器架设笔记五-APACHE服务器配置(4)
- mac python3 安装mysqlclient
- java 搭建企业应用框架_溯源微服务开发体系:一位Java开发者的转型思考
- 个性化推荐系统该如何评估,四种不同策略的角度
- Qt 设置应用程序图标
- Extjs TextField扩展
- 前端学习(742):arguments使用
- Sphinx+Scws 搭建千万级准实时搜索应用场景详解
- python字典遍历 没有顺序_Python实现字典的遍历与排序功能示例
- 常用HDFS java API
- 原地踏步 – 没有突破高效理念的结果
- 高性能服务框架-Dubbo实战教程,看这一篇就够了
- windows xp输入法设置空白解决的方法
- 测试ai模糊软件,“马赛克”视频能被AI软件彻底还原?测试结果出乎意料!
- Arduino UNO测试BME280温湿度气压传感器
- logback springBoot 配置日志
- 【微商】我和99%的人观点不同
- PMP中各种图形解释和使用场景
- matlab电机系统建模与仿真软件下载,基于MATLAB直流无刷电动机系统建模与仿真
- 第二届邯郸钢铁展会,图扑软件荣获“2022钢铁行业智造之星奖”