因为公司的一个项目使用了WebService,虽然我们只提供后台接口,但是总想掌握一下其中的流程,因此最近两天试着做了一下,总共有四个demo,最后一个就是这次要发表的,把其中的详细给说一下。
一、创建服务类方法
1.首先创建工程:JaxWSProject
上图是创建完成后的工程结构,其中wsdl、wssrc为自建目录,主要为服务端创建wsdl、xsd文件及response.java文件使用。
创建GetInfo服务类,这个类中只有一个方法用于接收传递过来的参数,并调用存储过程查询相关信息后,再返回对象,代码如下
package ctsjavacoe.ws.fromjava.service; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.sqlException; import java.sql.Types; import javax.jws.WebMethod; import javax.jws.WebService; import ctsjavacoe.ws.fromjava.bean.PolicyInfo; @WebService public class GetInfo { @WebMethod public PolicyInfo getPolicy(PolicyInfo policy_info) { Connection conn = null; CallableStatement proc = null; PolicyInfo rtninfo = new PolicyInfo(); try { Class.forName("oracle.jdbc.driver.OracleDriver"); try { // 创建连接 conn = DriverManager.getConnection( "jdbc:oracle:thin:@xxx.xxx.xxx.xxx:xxx:xxxx","xxxx","xxxxx"); // 调用pk proc = conn .prepareCall("{call PK_NB_DATACHECK.get_po_status_code(?,?,?)}"); // 设置参数 proc.setString(1,policy_info.getAppl_no()); proc.registerOutParameter(2,Types.VARCHAR); proc.registerOutParameter(3,Types.VARCHAR); proc.registerOutParameter(4,Types.VARCHAR); proc.registerOutParameter(5,Types.SMALLINT); // 执行调用 proc.execute(); // 设置返回参数 policy_info.setIs_ybt(proc.getInt(5)); policy_info.setPolicy_channel(proc.getString(4)); policy_info.setPolicy_no(proc.getString(2)); policy_info.setPolicy_status(proc.getString(3)); // 关闭连接 if (null != proc) { proc.close(); } if (null != conn) { conn.close(); } } catch (sqlException e) { e.printstacktrace(); } } catch (ClassNotFoundException e) { // e.printstacktrace(); System.out.println("oracle驱动出错"); } return policy_info; } }其中两处@注解是为了说明这个类及方法,为webservice调用服务
打开命令窗口输入以下命令,并执行(此处需要先安装JAX-WS相关jar包,以便能够系统能识别命令),具体信息如图:
命令成功执行后,在工程里刷新后在wsdl及wssrc目录下会得到相应的文件,如图:
将javxws包下的两个源文件剪切到src相应的目录里,注意这里应该剪切。
之后更改web-inf目录下的sun-jaxws.xml文件,将此webservice配置进去。代码如下:
<endpoint name='GetInfo' implementation="ctsjavacoe.ws.fromjava.service.GetInfo" url-pattern="/GetInfoService" />配置web.xml文件,使得能够正确请求到,配置信息如下:
<servlet> <servlet-name>GetInfo</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>GetInfo</servlet-name> <url-pattern>/GetInfoService</url-pattern> </servlet-mapping>如果xml里没有配置过webservice监听器,也需要进行配置,否则会报404错误,配置信息如下:
<listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSservletcontextlistener </listener-class> </listener>
发布工程后,请求 http://localhost:8080/JaxWSProject/GetInfoService?wsdl如果能够看到正常的wsdl展示信息(如下),则说明服务端已经完成。
二、创建客户端方法
1.创建一个java工程JaxWSClient 目录如下:
其中有些文件夹需要自建,此时需要把服务端创建好的wsdl、xsd文件给放到客户端的wsdl文件夹中,binding用来存放wsdl文件声明的xml。
其中binding文件夹中的GetPolicyInfo.xml配置内容如下
<?xml version="1.0" encoding="UTF-8"?> <bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="../wsdl/GetInfoService.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws"> <bindings node="wsdl:deFinitions"> <enableAsyncMapping>true</enableAsyncMapping> </bindings> </bindings>
打开命令窗口执行以下命令,用来生成如下图中标注的客户端调用文件
wsimport -keep -b binding/GetPolicyInfo.xml -d bin -s src wsdl/GetInfoService.wsdl
此时可以编写一个测试方法来调用webservice。具体代码如下:
package ctsjavacoe.ws.fromjava.client; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import javax.xml.ws.Response; import ctsjavacoe.ws.fromjava.service.GetInfo; import ctsjavacoe.ws.fromjava.service.GetInfoService; import ctsjavacoe.ws.fromjava.service.GetPolicyResponse; import ctsjavacoe.ws.fromjava.service.PolicyInfo; public class GetPolicyClient { public static void main(String[] args) { PolicyInfo g_policy_info = new PolicyInfo(); g_policy_info.setApplNo("100000000000612"); GetInfoService service = new GetInfoService(); GetInfo port = service.getGetInfoPort(); Response<GetPolicyResponse> getPolicyInfo = port.getPolicyAsync(g_policy_info); PolicyInfo rtnList = new PolicyInfo(); try { GetPolicyResponse getPolicyResponse = getPolicyInfo.get(); rtnList = getPolicyResponse.getReturn(); System.out.println(rtnList.getApplNo()); System.out.println(rtnList.getIsYbt()); System.out.println(rtnList.getPolicyChannel()); System.out.println(rtnList.getPolicyStatus()); System.out.println(rtnList.getPolicyNo()); } catch (InterruptedException e) { e.printstacktrace(); } catch (ExecutionException e) { e.printstacktrace(); } } }
运行后查看效果
运行成功,耶
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。