IDE:NetBeans8.0html
項目結構:java
(1)新建接口Hello.javaweb
package com.test; /** * @author y * @date 2015-9-5 7:51:29 * @version 1.0 * @desc */ public interface Hello { String sayHello(String name); }
(2)新建實現類HelloManager.javaapache
package com.test; /** * @author y * @date 2015-9-5 7:51:59 * @version V1.0 * @desc */ public class HelloManager implements Hello{ public String sayHello(String name) { return "Hello,"+name; } }
(3)配置web.xmlsession
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <listener> <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class> </listener> <servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet> <servlet-name>AdminServlet</servlet-name> <display-name>Axis Admin Servlet</display-name> <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <!--/services/*--> <url-pattern>/*</url-pattern> </servlet-mapping> <session-config> <!-- Default to 5 minute session timeouts --> <session-timeout>5</session-timeout> </session-config> <!-- currently the W3C havent settled on a media type for WSDL; http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft for now we go with the basic 'it's XML' response --> <mime-mapping> <extension>wsdl</extension> <mime-type>text/xml</mime-type> </mime-mapping> <mime-mapping> <extension>xsd</extension> <mime-type>text/xml</mime-type> </mime-mapping> <welcome-file-list id="WelcomeFileList"> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.jws</welcome-file> </welcome-file-list> </web-app>
(4)新建server-config.wsddapp
<?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"> <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> <service name="HelloManagerServices" provider="java:RPC"> <parameter name="className" value="com.test.HelloManager"/> <parameter name="allowedMethods" value="*"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> </requestFlow> </transport> </deployment>
成功部署以後訪問:http://localhost:7001/ws01/HelloManagerServices?wsdljsp
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://localhost:7001/ws01/HelloManagerServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:7001/ws01/HelloManagerServices" xmlns:intf="http://localhost:7001/ws01/HelloManagerServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!--WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT)--> <wsdl:message name="sayHelloRequest"> <wsdl:part name="name" type="soapenc:string"/> </wsdl:message> <wsdl:message name="sayHelloResponse"> <wsdl:part name="sayHelloReturn" type="soapenc:string"/> </wsdl:message> <wsdl:portType name="HelloManager"> <wsdl:operation name="sayHello" parameterOrder="name"> <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/> <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloManagerServicesSoapBinding" type="impl:HelloManager"> <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHello"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="sayHelloRequest"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://test.com" use="encoded"/> </wsdl:input> <wsdl:output name="sayHelloResponse"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:7001/ws01/HelloManagerServices" use="encoded"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloManagerService"> <wsdl:port binding="impl:HelloManagerServicesSoapBinding" name="HelloManagerServices"> <wsdlsoap:address location="http://localhost:7001/ws01/HelloManagerServices"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
客戶端測試:ide
public static void main(String[] args) throws Exception { String namespace="http://test.com"; String method="sayHello"; String wsdl="http://localhost:7001/ws01/HelloManagerServices"; Object[] params={"yshyee"}; String result = AxisInvokeJAXWSUtil.invoke(namespace, method, wsdl, params); System.out.println("=======result:"+result); }
AxisInvokeJAXWSUtil.java
public final class AxisInvokeJAXWSUtil { /** * * @param namespace * @param method * @param wsdl * @param params * @return * @throws Exception */ public static String invoke(String namespace, String method, String wsdl, Object[] params) throws Exception{ Object obj; Service service = new Service(); Call call = (Call) service.createCall(); call.setOperationName(new QName(namespace, method)); call.setTargetEndpointAddress(wsdl); if (null != params) { int len = params.length; for (int i = 0; i < len; i++) { //動態設置參數,並且參數類爲字符串類型 call.addParameter("arg" + i, XMLType.XSD_STRING, ParameterMode.IN); } if (len > 0) { //設置返回值類型爲字符串類型 call.setReturnType(XMLType.XSD_STRING); } } else { params = new Object[]{}; } obj = call.invoke(params); return obj == null ? "" : obj.toString(); } }