Visual Studio/Eclipse调用 JBoss5中的WebService
1. HelloWebService.java
package com.xx.webservices; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public class HelloWebService { @WebMethod @WebResult(name="result") public String sayHello(@WebParam(name="name")String name) { System.out.println("sayHello:" + name); return "你好,JBoss5 WebService 欢迎你:[" + name + "]"; } @WebMethod @WebResult(name="result") public int add(@WebParam(name="a")int a, @WebParam(name="b")int b) { return a + b; } }
2. web.xml
<servlet> <servlet-name>Hello</servlet-name> <servlet-class>com.xx.webservices.HelloWebService</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
3. VS添加WebService引用后, Progam.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloJBossService { class Program { static void Main(string[] args) { JBossHelloServiceRef.HelloWebServiceClient client = new JBossHelloServiceRef.HelloWebServiceClient(); JBossHelloServiceRef.add reqAdd = new JBossHelloServiceRef.add(); reqAdd.a = -11; reqAdd.b = 22; var rspAdd = client.add(reqAdd); var ret = rspAdd.result; Console.WriteLine(ret); //for (int i = 0; i < 10; i++) //{ JBossHelloServiceRef.sayHello reqSay = new JBossHelloServiceRef.sayHello(); reqSay.name = "张三"; var rspSay = client.sayHello(reqSay); Console.WriteLine(rspSay.result); //} Console.ReadKey(); } } }
Java端调用方法:
1. 在${jboss_home}/bin目录,在cmd中执行:
D:\DevPrograms\jboss\jboss-5.1.0.GA\bin>wsconsume.bat -k http://localhost:8080/HelloWorldJBoss5/hello?wsdl
生成代理文件.
2. 复制到java项目中
3. 添加测试代码
public class TestMain { /** * @param args */ public static void main(String[] args) { HelloWebServiceService svcProxy = new HelloWebServiceService(); HelloWebService svc = svcProxy.getHelloWebServicePort(); int c = svc.add(11, 22); System.out.println(c); String ret = svc.sayHello("吴xx11"); System.out.println(ret); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。