Monday, January 3, 2011

Develop Web Service With Axis2 #6 - Workaround Solution for Custom "Header" on Skeleton/Stub side

Prev >>> Develop Web Service With Axis2 #5 - Custom Soap Header on Stub side 

It is not straightforward to add <soap:header> to response message on skeleton in axis2.
Someone suggests to write own message receiver, or add soap header by module, and someone suggests to convert header information to HTTP header,etc.


There is a workaround solution to put header information to body. Let's start with code-first approach.

---- define header information

package com.test.axis.bean;

import java.util.Date;

public class Header {

    private String requestId;

   // setter and getter methods
}

----- re-define the interface

package com.test.axis.service;

import com.test.axis.bean.AuthUserRequest;
import com.test.axis.bean.UserInfoRequest;
import com.test.axis.bean.UserInfoResponse;
import com.test.axis.bean.WebServiceFault;

public interface UserServices {
    UserInfoResponse getUserInfo(UserInfoRequest infoRequest) throws WebServiceFault;

    UserInfoResponse authUser(
            AuthUserRequest authRequest)
            throws WebServiceFault;
}

---- define userInfoRequest by this way

package com.test.axis.bean;

public class UserInfoRequest {

   private Header header;
    private String userId;
   
    // setter and getter methods
 }

------ re-define UserInfoResponse object, put header inside as well

package com.test.axis.bean;

import java.util.Date;

public class UserInfoResponse {

    private Header header;
   private String userName;
 
    // setter and getter methods
}

------ generate wsdl based on above java beans and interface

------ request message

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ns2:getUserInfo xmlns:ns2="http://axis.test.com/ws">
            <ns2:userInfoRequest>
                <header xmlns="http://bean.axis.test.com/xsd">
                    <requestId>552121</requestId>
                </header>
                <ns1:userId xmlns:ns1="http://bean.axis.test.com/xsd">user111</ns1:userId>
            </ns2:userInfoRequest>
        </ns2:getUserInfo>
    </soapenv:Body>
</soapenv:Envelope>

------ response soap message

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns2:getUserInfoResponse xmlns:ns2="http://axis.test.com/ws">
<ns2:userInfoResponse>
<header xmlns="http://bean.axis.test.com/xsd">
<requestId>552121</requestId>
</header>
<ns1:userName xmlns:ns1="http://bean.axis.test.com/xsd">dddddddddd</ns1:userName>
</ns2:userInfoResponse>
</ns2:getUserInfoResponse>
</soapenv:Body>
</soapenv:Envelope>

No comments:

Post a Comment