【问题标题】:How do I change the server name in the links in a Spring RepositoryRestResource?如何更改 Spring RepositoryRestResource 中链接中的服务器名称?
【发布时间】:2017-04-09 14:41:43
【问题描述】:

我有一个基于 spring.io 指南的简单 RepositoryRestResource

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>{

    List<Person> findByLastName(@Param("name")  String lastName);

}

这确实有效,因为我的 REST 客户端返回

{
  "_links" : {
    "people" : {
      "href" : "http://127.0.0.1:8080/people{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://127.0.0.1:8080/profile"
    }
  }
}

但我的问题是,_links 的 http://127.0.0.1:8080 部分来自哪里,我该如何更改它?当我的应用程序投入生产(或我的任何环境,如本地或开发)时,我宁愿能够看到类似 http://api.mydomain.com 的内容。

如果这很重要,我将使用mvn package 编译我的程序并将其作为独立的 jar 在服务器上运行。

【问题讨论】:

    标签: java spring spring-boot spring-rest


    【解决方案1】:

    使用HttpServletRequest#getRequestURL(或getRequestURI)计算您的URL 的主机和端口。所以它在执行请求时总是会反映客户端使用的主机和端口。

    将其部署为 api.yourdomain.com 后,您将在链接 URL 中获得该内容。

    【讨论】:

    • 确实如此!我被甩了,因为当我们部署时,我们实际上是在 Docker 容器中部署,前面有一个 nginx 反向代理服务器,所以它总是看起来像 localhost:8080 到 Spring。我根据stackoverflow.com/questions/40791634/… 更改了 nginx 配置,它按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多