1. WSDL文件简介
W3School对此的简介是:WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言。WSDL 文档仅仅是一个简单的 XML 文档。它包含一系列描述某个 web service 的定义。
了解更多关于WSDL教程,点击http://www.w3school.com.cn/wsdl/index.asp了解。
2. 两种创建WSDL文件的方法
我们经常会使用别人开发好的WSDL 文档开发应用,有时候我们也会自己也可以开发WebService应用,对外提供接口。而开发WSDL 文档的方式有两种:JDK自带的工具创建和使用CXF框架创建。JDK版本版本必须为1.6及以上。
3. 开发流程:下面用例子说明:
3.1 使用JDK自带工具创建:
接口类:IUserService
package com.nice.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface IUserService {
@WebMethod
//返回一个字符串
String getStr(String str);
}
实现类:UserServiceImpl
package com.nice.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class UserServiceImpl implements IUserService{
@WebMethod
@Override
public String getStr(String str) {
return "处理后的字符串:" + str;
}
}
测试类:TestServer
其实际上PATH(即想要生成WSDL 文件的地址可以自定义,端口号也可以自选。)
package com.nice.server;
import com.nice.interceptor.VerrifyInInterceptor;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.message.Message;
import javax.xml.ws.Endpoint;
import java.util.List;
public class TestServer {
private static String PATH = "http://127.0.0.1:7777/WebService_Server/jdk";
//浏览器输入以上地址访问不到,但是http://127.0.0.1:7777/WebService_Server/jdk?wsdl这个地址可以查看到xml文件
//相对路径随便写
public static void main(String[] args) {
//获取一个EndPoint实例
EndpointImpl endpoint = (EndpointImpl) Endpoint.publish(PATH, new UserServiceImpl());
//服务器端获取输入拦截器
List<Interceptor<? extends Message>> inInterceptors = endpoint.getInInterceptors();
//添加接受信息日志拦截器
inInterceptors.add(new LoggingInInterceptor());
System.out.println("成功发布!");
}
}
运行之后,在浏览器输入地址http://127.0.0.1:7777/WebService_Server/jdk?wsdl可以看到以下xml文件:
3.2 CXF框架创建:
首选导入CXF相关依赖,或者直接在本地文件夹中导入CXF相关jar包。(如果是父项目下面包含多个子项目,则父子项目都必须导入依赖包)。
接口类、实体类不变,还是用以上类。测试类写法有变化:
测试类:TestServer
package com.nice.server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class TestServer {
public static void main(String[] args) {
//以下是编译wsdl文件的另一种方法,这种方法使用CXF的JaxWsServerFactoryBean类中的方法创建
System.out.println("启动服务器...");
USerServiceImpl uSerService = new USerServiceImpl();
//创建地址
String address = "http://127.0.0.1:7777/WebService_Server/cxf";
//创建一个服务器工厂实例
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
//设置暴露地址
jaxWsServerFactoryBean.setAddress(address);
//设置接口类
jaxWsServerFactoryBean.setServiceClass(IUser.class);
//设置实现类
jaxWsServerFactoryBean.setServiceBean(uSerService);
//添加日志拦截器
jaxWsServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
//创建服务器
jaxWsServerFactoryBean.create();
System.out.println("服务器成功启动!");
}
}
运行之后,在浏览器输入地址http://127.0.0.1:7777/WebService_Server/cxf?wsdl可以看到以下xml文件:
生成WSDL 文件之后,就可以进行下一步开发,例如客户端的开发。