SpringBoot重定向时报错

bridge
2021-03-09 / 0 评论 / 3 点赞 / 817 阅读 / 895 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-01-19,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1.重定向时报错

javax.servlet.ServletException: Could not resolve view with name ‘redirect:https:xxx'

2.复现

import com.jjxx.qrcode.service.QrCodeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description:
 * @Author: liguanqiao
 * @Date: 2021/3/8 下午4:02
 **/
@Controller
@RequestMapping("/open")
public class Controller {

    @RequestMapping("/{var}")
    public String scan(@PathVariable("var") String var) {
        return "redirect:https://www.baidu.com";
    }

}

3.解决办法

  • 3.1.配置Spring自带的内部资源解析器:InternalResourceViewResolver
@Bean
public InternalResourceViewResolver viewResolver(){
    return new InternalResourceViewResolver();
}
  • 3.2.使用HttpServletResponse response重定向的方法:
response.sendRedirect(url);
0

评论区