How To Log SOAP Request and Response with Log4j in Axis 1.x

Sometimes we want to log the request that we send to server and the response from server as well. Log4j is one of the library that can help us for this kind of requirement.

First, create class that extend BasicHandler class and implements the invoke method just like this.


package com.ariestania.client.logger;

import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.commons.logging.LogFactory;

/**
 * @author ariestania.winda
 */
public class SoapLogHandler extends BasicHandler {

    @Override
    public void invoke(MessageContext mc) throws AxisFault {
        if (mc.getResponseMessage() != null && mc.getResponseMessage().getSOAPPartAsString() != null) {
            String resMsg = mc.getResponseMessage().getSOAPPartAsString();
            //print response on console
            System.out.println("Response: \r\n" + resMsg);
            //print response on log file
            LogFactory.getLog("bohayMessage").info("Response: " + resMsg);
        } else if (mc.getRequestMessage() != null && mc.getRequestMessage().getSOAPPartAsString() != null) {
            String reqMsg = mc.getRequestMessage().getSOAPPartAsString();
            //print request on console
            System.out.println("Request: \r\n" + reqMsg);
            //print request on log file
            LogFactory.getLog("bohayMessage").info("Request: " + reqMsg);
        }
    }
}

Continue reading “How To Log SOAP Request and Response with Log4j in Axis 1.x”

Advertisements

How To Create Simple Web Service Using Netbeans as IDE

This time, I use Netbeans 8.1 and tomcat 8.0.27 as the server. You can choose other version if you want.

Here is step by step to create the web service using Netbeans as the IDE:

Create new java web project. Choose the server and java EE version that you want. Click Finish.

Right click on the project then choose new -> web service. Here my project hierarchy after add new web service.

Add the operation by right click on web service class -> add operation.


package com.ariestania.example;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 * @author ariestania.winda
 */
@WebService(serviceName = "HelloBohayWebService")
public class HelloBohayWebService {

    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    @WebMethod(operationName = "doSummary")
    public int doSummary(@WebParam(name = "valOne") int valOne, @WebParam(name = "valTwo") int valTwo) {
        return valOne + valTwo;
    }

}

Continue reading “How To Create Simple Web Service Using Netbeans as IDE”