axis WebService虽然现在已经很少使用,但是还是把它的配置过程写出来,开发环境jdk 1.6
服务端:
1,导入需要jar包,自行下载
2,创建WebService接口
public interface Service {
public abstract String sayHello(String name);
}
3,接口实现:(用于测试,每次将调用传入的参数写入文件)
import java.io.FileWriter;
import com.sample.axis.service.interfacesImp.Service;
public class ServiceImp implements Service {
public String sayHello(String name) {
FileWriter fw = null;
try {
fw = new FileWriter("D:\\test.txt", true);
String tempString= name+"\r\n";
fw.write(tempString);
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fw !=null){
try {
fw.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
System.out.println(name);
return name;
}
}
4,修改Web.xml文件,添加如下内容:
<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-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
5,编辑server-config.wsdd文件,测试用的文件内容如下:
<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
<!-- note that either style="message" OR provider="java:MSG" both work -->
<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>
<service name="HelloService" provider="java:RPC">
<parameter name="className" value="com.sample.axis.service.interfaces.ServiceImp" />
<parameter name="allowedMethods" value="*" />
</service>
<transport name="http">
<requestFlow>
<handler type="java:org.apache.axis.handlers.http.URLMapper"/>
</requestFlow>
</transport>
</deployment>
配置步骤大致如上,部署到测试web容器。访问http://localhost:8090/axis-webservice/services/HelloService?wsdl,可以看到如下内容:
客户端:
客户端调用过程:
public static void main(String[] args) {
String endPoint = "http://localhost:8090/axis-webservice/services/HelloService?wsdl";
try {
//通过正向代理调用webservice方法,需配置如下内容
// AxisProperties.setProperty("http.proxyHost", "1.1.1.1"); //代理IP
// AxisProperties.setProperty("http.proxyPort", "8095"); //代理端口
// AxisProperties.setProperty("http.proxyUser", "test"); //用户名
// AxisProperties.setProperty("http.proxyPassword", "12345");
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new URL(endPoint));
//设置targetNameSpace
call.setOperationName("sayHello");
//设置传入参数类型
call.addParameter("name",XMLType.XSD_STRING,ParameterMode.IN);
//设置返回参数类型,此处设置为String,如果是void则是:org.apache.axis.encoding.XMLType.AXIS_VOID
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING);
//传入参数,并获取返回值
String result = call.invoke(new Object[]{"huyaozhong1"});
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}