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);
评论区