使用给定的应用程序
@ApplicationPath("/api")
public class MyApplication {
}
Run Code Online (Sandbox Code Playgroud)
UriInfo#getBaseUri给了我一个应用程序路径。
@Context
private UriInfo uriInfo
uriInfo.getBaseUri(); // http://address/<context-path>/api
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得上下文路径?
如何获取上下文路径的完整 URL?
http://address/<context-path>
Run Code Online (Sandbox Code Playgroud)
更新
我目前使用这个答案中的代码。
@Context
private HttpServletRequest servletRequest;
final URI contextUri
= URI.create(servletRequest.getRequestURL().toString())
.resolve(servletRequest.getContextPath());
Run Code Online (Sandbox Code Playgroud)
还有其他建议吗?
要获取应用程序上下文,您可以在 REST 方法中注入 ServletContext 并从中检索 contextPath,例如:
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Catalog find(@PathParam("id") Long id, @Context ServletContext servletContext) {
String contextPath = servletContext.getContextPath();
...
}
Run Code Online (Sandbox Code Playgroud)
编辑
获取所需的“上下文路径的完整 URL”。您还可以使用 @Context 注释注入 HttpServletRequest 并使用getScheme()、getServerName()和getServerPort()方法来构建它。
| 归档时间: |
|
| 查看次数: |
5057 次 |
| 最近记录: |