在Spring Boot中,你可以使用RedirectView类来进行重定向。下面是一个使用Java代码配置重定向的示例:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

@RestController
public class MyController {

    @GetMapping("/")
    public RedirectView redirectToNewUrl() {
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("https://www.example.com/new-url");
        return redirectView;
    }
}

在上面的示例中,我们创建了一个RedirectView对象,并将要重定向的URL设置为https://www.example.com/new-url。然后,我们通过将RedirectView对象作为方法的返回值来触发重定向。

当访问根路径/时,将会自动重定向到https://www.example.com/new-url

你还可以根据需要设置其他重定向相关的属性,例如修改重定向的HTTP状态码、添加请求参数等。具体可参考RedirectView类的文档

springboot中如何使用java代码配置重定向

原文地址: http://www.cveoy.top/t/topic/hWFk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录