You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Play Framework跨主机重定向问题求助(Scala 2.11+Play 2.5)

解决Play Framework 2.5跨主机重定向问题

你的问题出在相对路径与绝对路径的区别:直接传入"localhost:3003/localConclusion"时,Play会把它当作当前请求路径的相对路径,所以会拼接到你当前服务的URL(http://localhost:9090/localConclusion/)后面,导致生成错误的跳转地址。

正确的解决方式

  • 方式1:使用带协议的完整绝对URL
    直接在跳转地址前加上http://(或https://),让Play识别为外部绝对地址:

    Redirect("http://localhost:3003/localConclusion")
    
  • 方式2:用Play的Call类构建外部请求(更规范)
    如果你需要更严谨地构造外部URL,可以使用Call类:

    import play.api.mvc.Call
    import play.api.mvc.Results.Redirect
    
    val externalRedirect = Call("GET", "http://localhost:3003/localConclusion")
    Redirect(externalRedirect)
    

额外建议

生产环境中不要硬编码localhost:3003这类地址,建议将外部服务地址配置到application.conf中,比如:

external.services.conclusion.url = "http://your-production-host:port/localConclusion"

然后在代码中通过配置读取:

import play.api.Configuration

class YourController(config: Configuration) extends Controller {
  def redirectAction = Action {
    val targetUrl = config.get[String]("external.services.conclusion.url")
    Redirect(targetUrl)
  }
}

内容的提问来源于stack exchange,提问作者Arsentiy Antipin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 09:39:32