前两篇关于使用Axis2开发WebService,都是使用了services.xml文件,而且还要拷贝axis2.war下面的文件到项目中,实际开发中是很麻烦的。
本篇简要讲述如何基于JAX-WS开发WebService的服务端,客户端如何调用请参考前几篇文章。
【1】编写接口与实现类
接口类如下:
package com.web.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface MyService {
@WebMethod
public String sayHello(String name);
}
实现类如下:
package com.web.service.impl;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.web.service.MyService;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MyServiceImpl implements MyService{
@Override
public String sayHello(String name) {
System.out.println("this is wsservice "+name);
return "hello "+name;
}
}
【2】xml配置
因为没有与Spring耦合,所以不需要对Spring进行配置。
需要的xml配置如下:
① 在WEB-INF下建立sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="wsService" implementation="com.web.service.impl.MyServiceImpl" url-pattern="/services/wsService" />
</endpoints>
该xml中只有一个endpoints元素,该元素下可以有多个endpoint ,每个endpoint 对应一个WebService,也就是说,可以发布多个WebService。
② 修改web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>wsService</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>wsService</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
或者web.xml如下:
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
如果说与Spring整合,那么只需要加入spring配置即可:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
【3】引入jar
没错,这个超级重要。不是说,没有jar不行,这里强调的是,你可能会遇到这种奇葩的找不到类的问题。
比如,这个类org/glassfish/gmbal/ManagedObjectManager
,是不是没见过?
然后去找,有的说,你只需要引入以下依赖就行了。
<dependencies>
<!-- JAXWS-RI -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.10</version>
</dependency>
</dependencies>
扯,扯,扯,可能对他真有用,但可能对你无济于事。
继续百度,有的说,需要management-api.jar。
怎么说呢,这个jar真心不好找,你可以试试。
即使,幸运,找到了,你以为就完了?
然而,并没有,还会有其他的类找不到。。。
幸运的是,遇到了我,下面是需要的jar与pom文件
这是服务端的jar截图,有些你可能不需要。
pom.xml如下:
<properties>
<axis2.version>1.6.2</axis2.version>
<jaxws.version>2.2.10</jaxws.version>
</properties>
<!-- axis2 包 -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb-codegen</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-codegen</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-java2wsdl</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-json</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-metadata</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-xmlbeans</artifactId>
<version>${axis2.version}</version>
</dependency>
<!-- JAXWS 包 -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jaxws.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>${jaxws.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>rt</artifactId>
<version>${jaxws.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>policy</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.gmbal</groupId>
<artifactId>gmbal-api-only</artifactId>
<version>3.0.0-b023</version>
</dependency>
<dependency>
<groupId>org.glassfish.gmbal</groupId>
<artifactId>gmbal-api-only</artifactId>
<version>3.0.0-b023</version>
</dependency>
还有一点,请使用中央仓库下载,因为最后面几个阿里云仓库根本没有,坑死我了!
温馨提示 : 上面的jar可能对你项目不完整,不过几个难缠的已经包括在内,其他的按需自行添加!
【4】发布Tomcat测试
正常启动如下图:
wsdl地址:
http://localhost:8080/Axis2WS/services/wsService?wsdl
就是你的正常项目访问路径+xml中配置的address+?wsdl
浏览器显示如下:
【5】客户端测试
① 根据wsdl,生成Stub到项目下。
② 编写客户端代码如下:
package com.web.client2;
import com.web.client2.MyServiceImplServiceStub.SayHello;
import com.web.client2.MyServiceImplServiceStub.SayHelloResponse;
public class Client {
public static void main(String[] args) throws Exception {
MyServiceImplServiceStub factory = new MyServiceImplServiceStub();
SayHello sayHello = new SayHello();
sayHello.setArg0("Tom");
SayHelloResponse response = factory.sayHello(sayHello );
String result = response.get_return();
System.out.println("Client "+result);
}
}
客户端输出结果如下:
服务端输出结果如下:
Client使用RPC方式如下:
package com.web.client;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class Client {
public static void main(String[] args) {
String url="http://localhost:8080/Axis2WS/services/wsService?wsdl";
String method="sayHello";
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
QName opAddEntry = new QName("http://impl.service.web.com/","sayHello");
Object[] opAddEntryArgs = new Object[] {"Tom"};
Class[] classes = new Class[] {String.class };
Object[] result=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes);
System.out.println(result[0].toString());
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
【6】多个WebService
就像上面说的一样,在sun-jaxws.xml中进行配置。
演示如下:
① 拷贝MyServiceImpl并重命名为MyServiceImpl2
② 修改sun-jaxws.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="wsService" implementation="com.web.service.impl.MyServiceImpl" url-pattern="/services/wsService" />
<endpoint name="wsService2" implementation="com.web.service.impl.MyServiceImpl2" url-pattern="/services/wsService2" />
</endpoints>
此时,第二个WebService对应wsdl地址为:
http://localhost:8080/Axis2WS/services/wsService2?wsdl
浏览器显示如下:
不用拷贝文件,不用配置services.xml是不是很爽?!