java实现webservice
第一步:web工程--新建server-config.wsdd 文件与web.xml同级
其内容如下
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="sendMultiRefs" value="true"/> <parameter name="disablePrettyXML" value="true"/> <parameter name="dotNetSoapEncFix" value="true"/> <parameter name="enableNamespacePrefixOptimization" value="false"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> </globalConfiguration> <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> <service name="ServiceMehtod.jws" provider="java:RPC"> <parameter name="className" value="com.fiis.application.sso.domain.ServiceMehtod"/> <parameter name="scope" value="request"/> <parameter name="allowedMethods" value="*"/> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> </requestFlow> </transport> </deployment>
第二步:WEB.XML可以直接从你下载的axis项目中拿来到自己工程就好,或者自己添加:
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
第三步:服务端提供的方法:
package server; public class SayHello { public String getName(String name) { return "hello====>" + name; } }
第四步:启动tomcat,先访问http://127.0.0.1:8080/webservice/services,(webservice为web工程名;services web.xml配置文件中的自定义名称)看是否有方法显示,有显示,则代表服务端提供的接口可以成功访问了。
效果如下:
And now... Some Services
- Login.jws (wsdl)
- getName
第五步:客户端访问服务端接口获得数据的方法:
package client; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestClient { public static void main(String[] args) throws Exception { // 指出service所在URL String endpoint = "http://127.0.0.1:8080/webservice/services/Login.jws"; // 创建一个服务(service)调用(call) Service service = new Service(); Call call = (Call) service.createCall();// 通过service创建call对象 // 设置service所在URL call.setTargetEndpointAddress(new java.net.URL(endpoint)); // 方法名(processService)与MyService.java方法名保持一致 call.setOperationName("getName"); // Object 数组封装了参数,参数为"This is Test!",调用processService(String arg) String ret = (String) call.invoke(new Object[] { "参数" }); System.out.println(ret); } }