我正在使用apache cxf将Java应用程序公开为REST api。
如何获取我的Java应用程序@GET方法中api调用者发送的标头详细信息
最佳答案
Apache CXF实现了JAX-RS规范。因此,您可以使用HttpHeaders
在资源类或资源方法中注入@Context
:
@Context
HttpHeaders httpHeaders;
然后,您可以使用
HttpHeaders
API来获取标头值:HttpHeaders#getHeaderString(String)
HttpHeaders#getRequestHeaders()
HttpHeaders#getHeaderString(String)
如果您需要标准HTTP标头的值,请考虑使用constants available in the
HttpHeaders
API:// Get the value of the Authorization header
String authorizationHeader = httpHeaders.getHeaderString(HttpHeaders.AUTHORIZATION);
有关上下文类型的更多信息,请参见Apache CXF documentation。