`
fortianwei
  • 浏览: 59700 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

axis2 发布WebService基础的基础

阅读更多

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> axis发布webservice有很多中方式,基于wsdl,wsdd等等,配置xml太麻烦,还是写点最最基础的:<!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

<!-- [if !supportLists]-->1.       <!-- [endif]-->写一个 java 类有一些基本函数

 

   public class Test{

    //fields

    private    String name="gaga";

    public String setName(String name){

    this.name=name;

    return "hello "+this.name;

    }

}
 

 

 

 

<!-- [if !supportLists]-->2.    <!-- [endif]-->axis 的完整包中有 webapps 文件夹,里面有一个 axis 文件夹,把它复制到 $TOMCAT_HOME/webapps/ 下,并将刚才写的 Test.java 改名为 Test.jws(java web service) 并放到 $TOMCAT_HOME/webapps/axis/ 下,启动 tomcat ,可以输入网址: http://localhost:8080/Test.jws?wsdl 查看他的 wsdl 文件,这是由 /webapps/axis 内的文件动态生成的。这个时候其实已经发布 web 服务了。

<!-- [if !supportLists]-->3.       <!-- [endif]-->编写 web service 客户端:(把 axis jar 包加到项目 classpath 中)

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

 

public class TestWebService{  

    public static void main(String args[]){      

        System.out.println("Start invoking....");      

         try {

             String endpoint =

                      "http://localhost:8080/axis/Test.jws";//你写的那个文件

             String newName=new String("tianwei");

             Service  service = new Service();

             Call     call    = (Call) service.createCall();       

             call.setTargetEndpointAddress( new java.net.URL(endpoint) );

             call.setOperationName("setName");//填写你要调用的方法名称

             //call.addParameter("name", XMLType.SOAP_STRING, ParameterMode.IN);

             //call.setReturnClass(String.class);

             //下面是来调用webservice函数的,Call已经绑定函数setName(String name)

             //了,new Object[]{newName}中newName是传入的参数。当然,大括号内可以有

             //不止一个参数

             String name=(String)call.invoke(new Object[]{newName});          

            System.out.println(name);

            

         } catch (Exception e) {

             System.err.println(e.toString());

         }       

        System.out.println("Finished the invoking.");       

       

    }

   

}
 

 

 

 

运行结果 :

Start invoking....

hello tianwei

Finished the invoking.

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics