如何更改 Spring RepositoryRestResource 中链接中的服务器名称?
How do I change the server name in the links in a Spring RepositoryRestResource?
我根据 spring.io
上的指南获得了一个简单的 RepositoryRestResource
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>{
List<Person> findByLastName(@Param("name") String lastName);
}
这确实有效,因为我的 REST 客户端 returns
{
"_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。
您的 URL 的主机和端口是使用 HttpServletRequest#getRequestURL(或 getRequestURI
)计算出来的。所以它会一直反映客户端在执行请求时使用的主机和端口。
将其部署为 api.yourdomain.com
后,这就是您将在 link URL 中获得的结果。
我根据 spring.io
上的指南获得了一个简单的 RepositoryRestResource@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>{
List<Person> findByLastName(@Param("name") String lastName);
}
这确实有效,因为我的 REST 客户端 returns
{
"_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。
您的 URL 的主机和端口是使用 HttpServletRequest#getRequestURL(或 getRequestURI
)计算出来的。所以它会一直反映客户端在执行请求时使用的主机和端口。
将其部署为 api.yourdomain.com
后,这就是您将在 link URL 中获得的结果。