java - How do i get the context path from a CXF REST service class -
i have war file run in tomcat. war contains number of html pages served (for testing purposes) http://localhost:port/testapp/somepage.html
.
included in app cxf rest service endpoint, hosted @ http://localhost:port/testapp/cxf/
few services http://localhost:port/testapp/cxf/getlink
the getlink method service should return link 1 of html pages. don't want statically set context path in code or in configuration file, have no control on context path application hosted at.
so want context path during runtime. how do this?
i have tried following (notice @path("/")
"cxf" part of path comes web.xml , cxf servlets path)
@path("/") public class testendpoint { ... @context uriinfo uri; @get @path("/getlink") public response givemexml(@context request context) { uri baseuri = uribuilder.fromuri(uri.getbaseuri()).replacepath("").build(); .... }
i expected uriinfo.getbaseuri()
give me uri containing "scheme://host:port/contextpath" of application, doesn't. returns "scheme://host:port/contextpath/cxf-app-path" http://localhost:8080/testapp/cxf
how context path war deployed under, in rest endpoint? want somehow context path war deployed under, like: http://localhost:8080/testapp/
.
unfortunately, afaict, there no single api obtain information. need manually (with string manipulation). 1 way inject httpservletrequest
, use its apis create path. example
@get public string getservletcontextpath(@context httpservletrequest request) { return getabsolutecontextpath(request); } public string getabsolutecontextpath(httpservletrequest request) { string requesturi = request.getrequesturl().tostring(); int endindex = requesturi.indexof(request.getcontextpath()) + request.getcontextpath().length(); return requesturi.substring(0, endindex); }
Comments
Post a Comment