java2wsdl+Axis2+hibernate开发webservice学习(1) - custom header

环境:

      Eclipse3.5

      Axis2 1.5.4

      Hibernate3.0

      Mysql5.0

      Tomcat6.0

 

一、创建dynamic web工程

      工程名eg:mywebservice.

      解压axis2.war,将所有的文件copy到/WebContent下,并引入hibernate 以及Mysql等相关必须的包。

 

二、创建service request 和response bean

      UserProfileRequest.java

      private RequestHeader header; private String name; public RequestHeader getHeader() { return header; } public void setHeader(RequestHeader header) { this.header = header; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; }

 

      RequestHeader.java

      private String systemId; private String requestId; private String msgTimestamp; public String getSystemId() { return systemId; } public void setSystemId(String systemId) { this.systemId = systemId; } public String getRequestId() { return requestId; } public void setRequestId(String requestId) { this.requestId = requestId; } public String getMsgTimestamp() { return msgTimestamp; } public void setMsgTimestamp(String msgTimestamp) { this.msgTimestamp = msgTimestamp;

 

      UserProfileResponse.java

      private ResponseHeader header; private String name; private String gender; private String email; public ResponseHeader getHeader() { return header; } public void setHeader(ResponseHeader header) { this.header = header; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } public String getGender() { return gender; } /** * @param gender * the gender to set */ public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }

 

      ResponseHeader.java

      private String systemId; private String requestId; private String msgTimestamp; public String getSystemId() { return systemId; } public void setSystemId(String systemId) { this.systemId = systemId; } public String getRequestId() { return requestId; } public void setRequestId(String requestId) { this.requestId = requestId; } public String getMsgTimestamp() { return msgTimestamp; } public void setMsgTimestamp(String msgTimestamp) { this.msgTimestamp = msgTimestamp; }

 

三、创建service

 

      OnepassServices.java

      public class OnepassServices { public UserProfileResponse getUserProfile( UserProfileRequest userProfileRequest) { return null; } }

 

四、生成wsdl

 

      进入build/class 目录下,运行如下命令:

 

      java2wsdl -cn com.axis2.service.OnepassServices -o ../../resource -of OnepassServices.wsdl -tn http://axis2.com/ws/service -stn com.axis2/ws -dlb doc/lit

 

     在工程目录下/resource就会生成

             --META-INf

                  ---services.xml

             --OnepassServices.wsdl

 

      注意:打开OnepassServices.wsdl需要修改部分的代码<xs:element />,比如args[0]等。贴出部分代码:

      <xs:complexType name="UserProfileRequest"> <xs:sequence> <xs:element minOccurs="0" name="header" nillable="true" type="ax21:RequestHeader"/> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="RequestHeader"> <xs:sequence> <xs:element minOccurs="0" name="msgTimestamp" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="requestId" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="systemId" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType>

 

五、根据wsdl生成skeleton

      进入工程目录下,运行如下命令:

    

      wsdl2java -uri resource/OnepassServices.wsdl -ss -sd -d adb -S ./src -R ./resource/META-INF -p com.axis2.ws.skeleton -ns2p http://bean.axis2.com/xsd=com.axis2.ws.bean.xsd,com.axis2/ws=com.axis2.ws.bean --noBuildXML -noWSDL -uw

 

       刷新工程,就会看到有类似如下代码生成:

        ---src

               ---com.axis2.ws.bean

               ---com.axis2.ws.bean.xsd

               ---com.axis2.ws.skeleton

                       --- OnepassServicesSkeleton.java


         OnepassServicesSkeleton.java这里就是我们要写入业务逻辑的地方。

 

         public com.axis2.ws.bean.xsd.UserProfileResponse getUserProfile ( com.axis2.ws.bean.xsd.RequestHeader header,java.lang.String name ) { IUserProfile iup = new UserProfileImpl(); User user = iup.getUser(name); UserProfileResponse uprs = new UserProfileResponse(); ResponseHeader reph = new ResponseHeader(); reph.setMsgTimestamp(header.getMsgTimestamp()); reph.setRequestId(header.getRequestId()); reph.setSystemId(header.getSystemId()); uprs.setHeader(reph); uprs.setGender(user.getGender()); uprs.setEmail(user.getEmail()); return uprs; }

 

 

六、生成客户端测试代码

         进入工程目录下,运行如下命令:

 

         wsdl2java -uri resource/OnepassServices.wsdl  -d adb -S src  -p junit.com.axis2.ws.stub  -noBuildXML

 

         ---junit.com.axis2.ws.stub

                 ---OnepassServicesCallbackHandler.java

                 ---OnepassServicesStub.java

 

七、创建测试代码(Junit Test)

                 ---OnePassTester.java

     OnepassServicesStub _stub; @Before public void setUp() throws Exception { try { _stub = new OnepassServicesStub( "http://192.168.1.147:8080/mywebservice/services/OnepassServices?wsdl"); // Options opt = _stub._getServiceClient().getOptions(); // HttpTransportProperties.Authenticator mbAuth = new // HttpTransportProperties.Authenticator(); // mbAuth.setUsername("username"); // mbAuth.setPassword("1q2w3e4r"); // mbAuth.setPreemptiveAuthentication(true); // opt.setProperty(HTTPConstants.AUTHENTICATE, mbAuth); // _stub._getServiceClient().setOptions(opt); } catch (AxisFault e) { e.printStackTrace(); } } @After public void tearDown() throws Exception { } @Test public void testGetUserProfile1() { GetUserProfileResult userProfileResult = new GetUserProfileResult(); UserProfileRequestE userProfileRequestE = new UserProfileRequestE(); UserProfileRequest upreq = new UserProfileRequest(); RequestHeader header = new RequestHeader(); UserProfileResponse userProfileResponse = new UserProfileResponse(); header.setRequestId("requestId"); header.setMsgTimestamp("2011-03-28 16:23:23"); header.setSystemId("systemId-123"); upreq.setHeader(header); upreq.setName("Robin"); userProfileRequestE.setUserProfileRequest(upreq); try { userProfileResult = _stub.getUserProfile(userProfileRequestE); userProfileResponse = userProfileResult.getGetUserProfileResult(); assertEquals("[email protected]", userProfileResponse.getEmail()); assertEquals("systemId-123", userProfileResponse.getHeader() .getSystemId()); } catch (RemoteException e) { System.out.println("aa"); e.printStackTrace(); } catch (Exception e) { System.out.println("bb"); e.printStackTrace(); } }

 

 

至此代码完成。下面说下部署,以Tomcat为例,其他server会有不同,比如weblogic相对复杂点,后面会有简单介绍。

 

八、将build/classes 下的skeleton copy到WebContent/WEB-INF/services下,如下结构:

 

        ---onewpassservice

                 ---com

                       ---...

                 ---META-INF

                       ---services.xml

 

九、部署到Tomcat下,并启动服务

 

十、初始化数据库

 

       运行dbscripts/init.data.sql

 

      insert into WS_USER(email_address,gender,user_name) values ('[email protected]','f','Robin');

 

 

十一、运行OnePassTester.java中的

 

         @Test

          testGetUserProfile1()

 

 

 如果一切顺利,将会看到可爱的green bar.

 

 

第二部分,会说明,如何增加我们自定义的module.

 

 

 

 

 

你可能感兴趣的:(java,Hibernate,exception,String,webservice,header)