部署时,Web 服务 URL 为 http://localhost:8080/[context-root]/[serviceName]。 context-root 是项目的名称,但仅显示在 WEB-INF 中的 sun-web.xml 中。 如何将这些信息传递给java层?我需要以编程方式获取 Web 服务的 url,而上下文根是目前唯一缺少的信息。
最佳答案
以下是您可以执行此操作的方法。假设此代码位于服务类中
@Resource
WebServiceContext wsContext;
@WebMethod
public String myServiceOperation(){
MessageContext context = wsContext.getMessageContext();
HttpServletRequest res = (HttpServletRequest) context.get(MessageContext.SERVLET_REQUEST) ;
System.out.println(" Context:path "+res.getContextPath());
}
这会将上下文路径打印到控制台。
关于java - 从 Web 服务中的 sun-web.xml 获取上下文根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10541654/