In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要介绍Spring Boot项目@RestController如何使用重定向redirect,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Spring Boot @RestController重定向redirect
Spring MVC项目中页面重定向一般使用return "redirect:/other/controller/";即可。而Spring Boot使用了@RestController注解,上述写法只能返回字符串
解决方法如下
将一个HttpServletResponse参数添加到处理程序方法然后调用response.sendRedirect("some-url");
@RestControllerpublic class FooController { @RequestMapping("/foo") void handleFoo(HttpServletResponse response) throws IOException { response.sendRedirect("some-url"); }}@RestController 注释下的重定向探讨背景
在工作中遇到需要@RestController 内部的方法直接进行重定向或者转发
我们知道@RestController注解后,方法默认返回json。但是如果返回ModelAndView 则依然按ModelAndView曾经的逻辑走
那么springmvc内部的逻辑是如何走的呢?HandlerMethodReturnValueHandlerComposite#selectHandler()HandlerMethodReturnValueHandlerComposite#handleReturnValue() ServletInvocableHandlerMethod#invokeAndHandle()RequestMappingHandlerAdapter#invokeHandlerMethod()RequestMappingHandlerAdapter#handleInternal() AbstractHandlerMethodAdapter#handle() DispatcherServlet#doDispatch()DispatcherServlet#doService()
自下向上调用,最终通过依据返回参数和MethodParameter(方法参数的简述类) 得到使用何种HandlerMethodReturnValueHandler,当我们加上@RestController 如果方法返回ModelAndView 或者 View自然返回ModelAndViewMethodReturnValueHandler去处理,因此就解决了需要在@RestController注解下转发或重定向的问题。
下面是springmvc 默认的15种HandlerMethodReturnValueHandler0 = {ModelAndViewMethodReturnValueHandler@7121} 1 = {ModelMethodProcessor@8372} 2 = {ViewMethodReturnValueHandler@8373} 3 = {ResponseBodyEmitterReturnValueHandler@8374} 4 = {StreamingResponseBodyReturnValueHandler@7125} 5 = {HttpEntityMethodProcessor@8375} 6 = {HttpHeadersReturnValueHandler@8376} 7 = {CallableMethodReturnValueHandler@8377} 8 = {DeferredResultMethodReturnValueHandler@8378} 9 = {AsyncTaskMethodReturnValueHandler@8379} 10 = {ModelAttributeMethodProcessor@8380} 11 = {RequestResponseBodyMethodProcessor@7132} 12 = {ViewNameMethodReturnValueHandler@8381} 13 = {MapMethodProcessor@8382} 14 = {ModelAttributeMethodProcessor@8383}以上是"Spring Boot项目@RestController如何使用重定向redirect"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.