我有一个像这样的简单网络服务:
@WebService
public class MyWebService
{
@WebMethod
public String ProcessQuery(@WebParam(name="query") String q)
{
// Logging here: User IP, etc.
}
public static void main(String[] args) throws Exception
{
String address = "http://127.0.0.1:8023/_WebServiceDemo";
Endpoint.publish(address, new MyWebService());
new DocumentServer();
System.out.println("Listening: " + address);
}
}
我想为我的服务添加一个日志记录方法来提取信息。我听说过 NCSA 格式和 Log4J,但我不知道如何在服务中使用它们。我想记录用户的 ip 和其他信息。我该怎么做?
谢谢。
编辑:我应该注意,我的问题的主要部分是如何在网络方法中检索一些数据,例如用户的 IP、客户端等。
最佳答案
将 WebServiceContext
添加到您的类中,这样您就可以获得 HttpServletRequest
:
@WebService
public class MyWebService
{
@Resource
WebServiceContext wsContext;
@WebMethod
public String ProcessQuery(@WebParam(name="query") String q)
{
MessageContext messageContext = wsContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest) messageContext.get(SOAPMessageContext.SERVLET_REQUEST);
// now you can get anything you want from the request
}
}
关于Java - 丰富的 Web 服务日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5625739/