1.Service类
- package com.zhao.grgroup.ws;
- @WebService
- @SOAPBinding(style=SOAPBinding.Style.RPC)
- public class Service {
- @Resource
- WebServiceContext wsContext;
- static Logger logger = Logger.getLogger(Service.class);
- /** 卸货委托书接收
- * @param xhwtsHeader 抬头部分
- * @param xhwtsItems 项目部分
- * @return 字符串 S 表示成功
- */
- @WebMethod(operationName = "xhwts")
- public String xhwts(@WebParam(name = "jcwtHeader")XhwtsHeaderBean[] xhwtsHeader,
- @WebParam(name = "jcwtItems")XhwtsItemBean[] xhwtsItems){
- //do things
- }
2.各种JavaBean类
包含属性和对应的getter、setter
3.设置项目的WEB-INF的sun-jaxws.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
- <endpoint
- name="Service" --name
- implementation="com.zhao.grgroup.ws.Service" --完整的包名+类名
- url-pattern="/export" --访问路径(跟web.xml中的一致)
- />
- <endpoint
- name="TcpsService"
- implementation="com.zhao.grgroup.ws.tcps.Service"
- url-pattern="/tcps-export"
- />
- </endpoints>
4.设置项目的web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>grgroup-ws</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>ProxoolConfigFile</param-name>
- <param-value>/WEB-INF/proxool.config</param-value>
- </context-param>
- <listener>
- <listener-class>com.zhao.common.db.ProxoolListener</listener-class>
- </listener>
- <listener>
- <listener-class>com.zhao.common.ConfigListener</listener-class>
- </listener>
- <listener>
- <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>jax-ws</servlet-name>
- <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>jax-ws</servlet-name>
- <url-pattern>/export</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>jax-ws-tcps</servlet-name>
- <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>jax-ws-tcps</servlet-name>
- <url-pattern>/tcps-export</url-pattern>
- </servlet-mapping>
- </web-app>
5.使用apache-cxf-2.7.0生成web service的client端访问类
- 1.进入目录 :
- D:\zhao\jee-juno\apache-cxf-2.7.0\bin
- 2.执行命令,获得client端代码
- D:\zhao\jee-juno\apache-cxf-2.7.0\bin>wsdl2java -client -encoding utf-8 -d D:\zhao\jee-juno\export(生成类的保存路径) http://localhost:8080/grgroup-ws/export?wsdl
- 3.client调用server
- Service_ServicePort_Client.java
6.client访问service
- public static void main(String args[]) throws java.lang.Exception {
- URL wsdlURL = ServiceService.WSDL_LOCATION;
- if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
- File wsdlFile = new File(args[0]);
- try {
- if (wsdlFile.exists()) {
- wsdlURL = wsdlFile.toURI().toURL();
- } else {
- wsdlURL = new URL(args[0]);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- //wsdlURL可配置
- //port可以循环利用?
- ServiceService ss = new ServiceService(wsdlURL, SERVICE_NAME);
- Service port = ss.getServicePort();
- System.out.println("Invoking tzckd...");
- com.pansontech.grgroup.ws.TzckdHeaderBeanArray _tzckd_tzckdHeader = null;
- _tzckd_tzckdHeader = new TzckdHeaderBeanArray();
- TzckdHeaderBean tableSapDrumHeavyOut = new TzckdHeaderBean();
- //基本信息
- tableSapDrumHeavyOut.setDjh("d");
- tableSapDrumHeavyOut.setZdrq("1900-4-1 00:00:05");
- tableSapDrumHeavyOut.setTzckdh("t");
- tableSapDrumHeavyOut.setCzck("c");
- tableSapDrumHeavyOut.setCzckms("c");
- tableSapDrumHeavyOut.setGc("c");
- tableSapDrumHeavyOut.setGcms("c");
- tableSapDrumHeavyOut.setXsddh("c");
- tableSapDrumHeavyOut.setSjxm("c");
- tableSapDrumHeavyOut.setSjsfz("c");
- tableSapDrumHeavyOut.setThdw("c");
- tableSapDrumHeavyOut.setCh("c");
- tableSapDrumHeavyOut.setBz("c");
- tableSapDrumHeavyOut.setShdz("c");
- _tzckd_tzckdHeader.getItem().add(tableSapDrumHeavyOut);
- com.pansontech.grgroup.ws.TzckdItemBeanArray _tzckd_tzckdItems = null;
- _tzckd_tzckdItems = new TzckdItemBeanArray();
- TzckdItemBean tableSapDrumHeavyOutDetail = new TzckdItemBean( );
- tableSapDrumHeavyOutDetail.setDjh("d");
- tableSapDrumHeavyOutDetail.setXmh("x");
- tableSapDrumHeavyOutDetail.setWlh("w");
- tableSapDrumHeavyOutDetail.setWlms("w");
- tableSapDrumHeavyOutDetail.setJhsl("1");
- tableSapDrumHeavyOutDetail.setJbjldw("2");
- tableSapDrumHeavyOutDetail.setKcdd("k");
- tableSapDrumHeavyOutDetail.setGg("3");
- _tzckd_tzckdItems.getItem().add(tableSapDrumHeavyOutDetail);
- tableSapDrumHeavyOutDetail = new TzckdItemBean( );
- tableSapDrumHeavyOutDetail.setDjh("d");
- tableSapDrumHeavyOutDetail.setXmh("x");
- tableSapDrumHeavyOutDetail.setWlh("w");
- tableSapDrumHeavyOutDetail.setWlms("w");
- tableSapDrumHeavyOutDetail.setJhsl("1");
- tableSapDrumHeavyOutDetail.setJbjldw("2");
- tableSapDrumHeavyOutDetail.setKcdd("k");
- tableSapDrumHeavyOutDetail.setGg("3");
- _tzckd_tzckdItems.getItem().add(tableSapDrumHeavyOutDetail);
- java.lang.String _tzckd__return = port.tzckd(_tzckd_tzckdHeader, _tzckd_tzckdItems);
- System.out.println("tzckd.result=" + _tzckd__return);
- }