1.前期准备
需要的jar包:(1)WSDL4J 下载;(2)Axis2就不多说了,学习笔记(1)里有介绍。
然后在Myeclipse里的工具栏选Window->Preferences->Java->Build Path->User Library中加入jar包,最后在项目(Project)的Preferences里也如此操作把jar包加入项目库中。
2.数据库设计
1.在MySQL下建立数据库wsdb;
2.建立service表
CREATE TABLE `wsdb`.`service` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`service_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
3.建立method表
CREATE TABLE `wsdb`.`method` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`method_name` VARCHAR(45) NOT NULL,
`service_id` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
4.建立message表(其中message_type中,0表示输入消息,1表示输出消息)
CREATE TABLE `wsdb`.`message` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`message_type` INTEGER UNSIGNED NOT NULL,
`message_name` VARCHAR(45) NOT NULL,
`method_id` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
3.部署数据库连接池(全局法)
1.%TOMCAT_HOME%\conf\server.xml中GlobalNamingResources结点中添加子结点,如下:
<Resource name="jdbc/wsdb" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/wsdb?characterEncoding=UTF-8"
username="root"
password="1234"
maxActive="200"
maxIdle="50"
maxWait="3000"/>
2.%TOMCAT_HOME%\conf\Catalina\localhost中添加WSDLdemo.xml对该项目进行配置:
<Context path="/WSDLdemo" docBase="WSDLdemo" debug="0">
<ResourceLink name="jdbc/wsdb" global="jdbc/wsdb" type="javax.sql.DataSource"/>
</Context>
4.创建JSP项目WSDLdemo并编写DBConn数据库连接基类
package Comm;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DBconn extends HttpServlet {
protected java.sql.Connection conn=null;
/**
* 返回结果集
* @param sql
* @param args
* @return
* @throws Exception
*/
protected java.sql.ResultSet execSQL(String sql,Object... args)
throws Exception
{
try
{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sql);
for(int i=0;i<args.length;i++)
{
pstmt.setObject(i+1, args[i]);
}
pstmt.execute();
return pstmt.getResultSet();
}
catch(Exception e)
{
return null;
}
}
/**
* 重写service
*/
@Override
protected void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
try
{
if(conn==null)
{
javax.naming.Context ctx=new javax.naming.InitialContext();
javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/wsdb");
conn=ds.getConnection();
}
}
catch(Exception e)
{
}
}
/**
* 清除DBconn
*/
@Override
public void destroy()
{
try
{
if(conn!=null)
{
conn.close();
}
}
catch(Exception e)
{
}
}
}
5.编写解析类Resolution
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.wsdl.*;
import javax.wsdl.extensions.*;
import javax.wsdl.factory.*;
import javax.wsdl.xml.*;
import javax.xml.namespace.QName;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import Comm.DBconn;
import java.sql.ResultSet;
import java.util.*;
public class Resolution extends DBconn {
@Override
protected void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
super.service(request, response);
String location="";
String sql="";
int service_id=0;
int method_id=0;
int i=0;//用于对操作进行循环编号
try
{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
//获得请求的wsdl地址
location=request.getParameter("wsdl_location");
//解析wsdl
WSDLFactory factory=WSDLFactory.newInstance();
WSDLReader reader=factory.newWSDLReader();
reader.setFeature("javax.wsdl.verbose",true);
reader.setFeature("javax.wsdl.importDocuments",true);
Definition def=reader.readWSDL(location);
//解析服务名
out.write("<br>");
out.write("[Service Name]:");
String tns="http://service.pojo.sample"; //targerNamespace
Service service =def.getService(new QName(tns,"WeatherService"));
out.write(service.getQName().getLocalPart());
out.write("<br>");
//入库,插信息进service table
String service_name=service.getQName().getLocalPart();
sql="insert into service(service_name) values (?)";
execSQL(sql,service_name);
//获取本service在数据库中的id,因为可能会存在同名的service
try
{
sql="select max(id) as id from service where service_name=?";
ResultSet rs=execSQL(sql,service_name);
rs.next();
service_id=rs.getInt("id");
}
catch(Exception e)
{
out.write(e.getMessage());
}
//解析接口方法名
Port port =service.getPort("WeatherServiceHttpEndpoint");
Binding binding=port.getBinding();
PortType portType=binding.getPortType();
//获取porttype下的operations
List<Operation> operations=portType.getOperations();
Iterator<Operation> operIter=operations.iterator();
while(operIter.hasNext())
{
Operation operation=operIter.next();
if(!operation.isUndefined())
{
i++;
//获取方法名,并打印至客户端
out.write("[Operation"+i+"]:"+operation.getName()+"<br>") ;
//入库,把方法信息插入method表
String method_name=operation.getName();
sql="insert into method(method_name,service_id) values (?,?)";
execSQL(sql,method_name,service_id);
//获取本method在数据库中的id,因为可能会存在同名的method
try
{
sql="select max(id) as id from method where method_name=?";
ResultSet rs=execSQL(sql,method_name);
rs.next();
method_id=rs.getInt("id");
}
catch(Exception e)
{
out.write(e.getMessage());
}
//获取该方法名下的信息(参数)
Input in=operation.getInput();
if(in!=null)
{
Message inputMsg=in.getMessage();
QName inputQName=inputMsg.getQName();
out.write("-Input Message:"+inputQName.getLocalPart()+"<br>");
//入库,把消息插入message表
String message_name=inputQName.getLocalPart();
sql="insert into message(message_type,message_name,method_id) values (?,?,?)";
execSQL(sql,0,message_name,method_id);
}
Output ot=operation.getOutput();
if(ot!=null)
{
Message outputMsg=ot.getMessage();
QName outputQName=outputMsg.getQName();
out.write("-Output Message:"+outputQName.getLocalPart()+"<br>");
//入库,把消息插入message表
String message_name=outputQName.getLocalPart();
sql="insert into message(message_type,message_name,method_id) values (?,?,?)";
execSQL(sql,1,message_name,method_id);
}
}
}
//解析服务地址
out.write("[Service location]:");
out.write("<br>");
List l=port.getExtensibilityElements();
ExtensibilityElement element=(ExtensibilityElement) l.get(0);
String s=element.toString();
out.write(s.substring(s.indexOf("location")));
}
catch(Exception e)
{
}
}
}
并对该servlet进行配置:
<servlet-mapping>
<servlet-name>Resolution</servlet-name>
<url-pattern>/resolution</url-pattern>
</servlet-mapping>
6.编写前台JSP页面:resolution.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<form name="resolution_form" action="resolution" method="post">
请输入WSDL地址以用于解析:
<input type="text" id="wsdl_location" name="wsdl_location"/>
<input type="submit" value="解析" name="resolution"/>
</form>
</body>
</html>
测试用wsdl地址:http://localhost:8080/axis2/services/WeatherService?wsdl
解析成功的结果如下:
[Service Name]:WeatherService
[Operation1]:setWeather
-Input Message:setWeatherRequest
[Operation2]:getWeather
-Input Message:getWeatherRequest
-Output Message:getWeatherResponse
[Service location]:
locationURI=http://localhost:8080/axis2/services/WeatherService.WeatherServiceHttpEndpoint/
并且已经把数据传入了数据库中