一、服务端代码
1、创建Maven工程
注意pom.xml文件的配置,需要引入axis的相关包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codefish</groupId>
<artifactId>javalab</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>javalab Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- axis 1.4 jar start -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
<!-- axis 1.4 jar end -->
</dependencies>
<build>
<finalName>javalab</finalName>
</build>
</project>
2、在web.xml中配置axis的servlet
<!-- axis 配置 -->
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>axis</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
3、写一个对外发布的接口
package com.codefish.javalab.ws.server;
public interface HelloService {
public String sayHello(String info);
}
4、写接口的实现类
package com.codefish.javalab.ws.server;
public class HelloServiceImpl implements HelloService {
public String sayHello(String info) {
// TODO Auto-generated method stub
return "sayHello:"+info;
}
}
5、通过server-config.wsdd文件对外发布服务
server-config.wsdd文件存放在工程的WEB-INFO目录下(与web.xml同级目录,这个文件axis框架底层会去解析的,不用操心怎么去加载)
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<service name="HelloServiceImpl" provider="java:RPC">
<parameter name="className" value="com.codefish.javalab.ws.server.HelloServiceImpl"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
6、验证
启动服务,在浏览器中键入:http://localhost:8080/javalab/services/HelloServiceImpl?wsdl
可以打开如下页面,表示发布成功:
二、客户端代码
1、仍然基于axis,建立HelloClient.java类:
package com.codefish.javalab.ws.client.hello;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class HelloClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
Service service = new Service();
try {
Call call = (Call)service.createCall();
//设置地址
call.setTargetEndpointAddress("http://localhost:8080/javalab/services/HelloServiceImpl?wsdl");
//设置要执行的方法(以下两种方式都可以)
// call.setOperationName("sayHello");
call.setOperationName(new QName("http://server.ws.javalab.codefish.com","sayHello"));
//设置要传入参数,如果没有要传入的参数,则不要写这个(参数名、参数类型、ParameterMode)
call.addParameter("info", org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
//设置返回的类型
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
//调用WebService服务
String info = "小鱼儿,你好!";
String result = (String) call.invoke(new Object[]{info});
System.out.println(result);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、运行上述代码,控制台中打印如下内容,表示调成功了
---------------------
作者:鱼多多
来源:CSDN
原文:https://blog.csdn.net/yuke07029306/article/details/79161568
版权声明:本文为博主原创文章,转载请附上博文链接!
来源:oschina
链接:https://my.oschina.net/u/4317706/blog/3561406