网络服务中的Java-RICH记录[英] Java - Rich logging in web services

本文是小编为大家收集整理的关于网络服务中的Java-RICH记录的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有这样的简单网络服务:

@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
    }

}

本文地址:https://www.itbaoku.cn/post/1575027.html

问题描述

I have a simple web service like this:

@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);
    }
}

I want to add a logging method for my service to extract information. I've heard about NCSA format and Log4J but I don't know how to use them in the service. I want to log user's ip and other info. How can I do it?

Thanks.

Edit: I should note that the main part of my question is how can I retrieve some data such as user's IP, client, etc. in the web method.

推荐答案

Add WebServiceContext to your class, so you can get the 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
    }

}